Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ import {
workflowOpeningParentThreadId,
} from "@t3tools/client-runtime/state/subagentRuntime";
import {
getUserQueuedThreadRuns,
threadCanSteer,
threadSupportsProviderHandoff,
} from "@t3tools/client-runtime/state/thread-workflows";
Expand Down Expand Up @@ -400,9 +399,15 @@ import { ChatRightPanel } from "./chat/ChatRightPanel";
import { useSplitRendered } from "../fork/rightPanelSplit/renderedSplit";
import { unfocusedPaneSurface } from "../fork/rightPanelSplit/splitLayout.logic";
import { useModeSurfaceActions } from "../fork/dashboard/dashboardSurface";
import { REPLY_BOX_QUEUE_EDIT_BLOCKED, subThreadDraftId } from "../fork/subThreads/subThreadReply";
import { subThreadDraftId } from "../fork/subThreads/subThreadReply";
import { useSubThreadCite } from "../fork/subThreads/subThreadCite";
import { SubThreadViewContext } from "../fork/subThreads/subThreadsState";
import {
QUEUE_EDIT_OPEN_ELSEWHERE,
useGuestMessagesInView,
useQueuedRunsInView,
useQueueShortcutLabels,
} from "../fork/subThreads/queuedRunsScope";
import { useThreadShownView } from "../fork/displayMode/threadView";
import { ChatWorkingIndicator } from "../fork/displayMode/ChatWorkingIndicator";
import {
Expand Down Expand Up @@ -559,12 +564,14 @@ export default function ChatView(props: ChatViewProps) {
const promptRef = useRef("");
const localComposerRef = useRef<ChatComposerHandle | null>(null);
const composerRef = useComposerHandleContext() ?? localComposerRef;
const queuedRunsInView = useQueuedRunsInView(routeThreadDetailRef, serverProjection);
const queuedRunEdit = useQueuedRunEdit({
environmentId,
routeThreadRef,
threadDraftTarget: baseComposerDraftTarget,
activeThreadId:
(serverThread !== null || draftThread !== null) && subThreadDraft === null ? threadId : null,
activeThreadId: serverThread !== null || draftThread !== null ? threadId : null,
ownsEdit: queuedRunsInView.ownsEdit,
recoveryTarget: queuedRunsInView.recoveryTarget,
serverProjection,
promptRef,
composerRef,
Expand Down Expand Up @@ -3215,7 +3222,7 @@ export default function ChatView(props: ChatViewProps) {
const onComposerCompactContextStable = useStableCallback(onCompactContext);
// Read from the projection the footer renders, so the scroll below sees the new message.
const pendingGuestMessageIds =
serverProjection?.thread.guestShare?.messages.flatMap((message) =>
useGuestMessagesInView(serverProjection?.thread.guestShare?.messages)?.flatMap((message) =>
message.status === "pending" ? [message.id] : [],
) ?? [];
const pendingGuestMessageCount = pendingGuestMessageIds.length;
Expand Down Expand Up @@ -3246,28 +3253,25 @@ export default function ChatView(props: ChatViewProps) {
openExpandedImagePreview(routeThreadKey, preview);
};
const onExpandTimelineImageStable = useStableCallback(onExpandTimelineImage);
const queueShortcutLabels = useQueueShortcutLabels(keybindings);
const queuedRunsControl =
isServerThread && activeThread ? (
<>
<QueuedRunsControl
ref={queuedRunsControlRef}
steerShortcutLabel={shortcutLabelForCommand(keybindings, "thread.steerQueuedMessage", {
context: { terminalFocus: false },
})}
editShortcutLabel={shortcutLabelForCommand(keybindings, "thread.editQueuedMessage", {
context: { composerFocus: true },
})}
steerShortcutLabel={queueShortcutLabels.steer}
editShortcutLabel={queueShortcutLabels.edit}
environmentId={activeThread.environmentId}
threadId={activeThread.id}
optimisticMessages={optimisticUserMessages}
editingRunId={queuedRunEdit.editingRunId}
editBlockedReason={
subThreadDraft === null
? queuedRunEditBlockedReason(
queuedRunEdit.isEditingElsewhere
? QUEUE_EDIT_OPEN_ELSEWHERE
: queuedRunEditBlockedReason(
{ openQuestionCount: pendingUserInputs.length },
messages,
)
: REPLY_BOX_QUEUE_EDIT_BLOCKED
}
onEditQueuedRun={queuedRunEdit.beginEdit}
onCancelEdit={queuedRunEdit.cancelEdit}
Expand Down Expand Up @@ -3322,7 +3326,7 @@ export default function ChatView(props: ChatViewProps) {
const shownThreadSyncPhase = activeEnvironmentUnavailable ? null : delayedThreadSyncPhase;
const hasThreadNow =
attentionNow !== null || workNow !== null || goalNow !== null || shownThreadSyncPhase !== null;
const queuedRunCount = serverProjection ? getUserQueuedThreadRuns(serverProjection).length : 0;
const queuedRunCount = queuedRunsInView.count;
// The queue control is mounted for every server thread but joins the footer only while it shows
// something, by its own rule: an optimistic message stops counting once the server has it.
const hasQueuedTurns =
Expand Down
16 changes: 13 additions & 3 deletions apps/web/src/components/chat/QueuedRunsControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
import { useId, useImperativeHandle, useRef, useState, type ReactNode, type Ref } from "react";

import { useAssetUrls } from "../../assets/assetUrls";
import { useShowsQueuedRun } from "../../fork/subThreads/queuedRunsScope";
import { threadEnvironment } from "../../state/threads";
import { useQuietThreadProjection, useQuietThreadShell } from "../../state/entities";
import { useEnvironment } from "../../state/environments";
Expand Down Expand Up @@ -121,7 +122,8 @@ export function QueuedRunsControl({
queuedImageAttachmentResources,
);
const workflow = projection ? deriveThreadQueueWorkflowState(projection, messages) : null;
const queued = workflow?.queuedRuns ?? [];
const showsQueuedRun = useShowsQueuedRun(scopeThreadRef(props.environmentId, props.threadId));
const queued = (workflow?.queuedRuns ?? []).filter(({ message }) => showsQueuedRun(message));
const activeRun = workflow?.activeRun ?? null;
// A fresh message starts its own turn: it may skip compaction, but not steer into a turn.
const canSendRun = (freshContext: boolean) =>
Expand Down Expand Up @@ -251,7 +253,14 @@ export function QueuedRunsControl({
};

const collapsible = items.length > QUEUE_VISIBLE_ROWS;
const visibleItems = collapsible && !showAll ? items.slice(0, QUEUE_VISIBLE_ROWS - 1) : items;
const visibleItems =
collapsible && !showAll
? items.filter(
(item, index) =>
index < QUEUE_VISIBLE_ROWS - 1 ||
(item.runId !== null && item.runId === props.editingRunId),
)
: items;

return (
<section
Expand All @@ -269,7 +278,8 @@ export function QueuedRunsControl({
})}
</span>
<ol className="flex flex-col items-end gap-3" id={queueListId}>
{visibleItems.map((item, position) => {
{visibleItems.map((item) => {
const position = items.indexOf(item);
const previewText = isAttachmentOnlyBootstrapPrompt(item.text)
? ""
: assistantCitationsToPlainText(
Expand Down
30 changes: 22 additions & 8 deletions apps/web/src/components/chat/useQueuedRunEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ export function useQueuedRunEdit(input: {
readonly routeThreadRef: ScopedThreadRef;
readonly threadDraftTarget: ComposerThreadTarget;
readonly activeThreadId: ThreadId | null;
readonly ownsEdit?: (context: OrchestrationMessageContext | undefined) => boolean;
readonly recoveryTarget?: (
context: OrchestrationMessageContext | undefined,
) => ComposerThreadTarget | undefined;
readonly serverProjection: ThreadStructure | null;
readonly promptRef: { current: string };
readonly composerRef: RefObject<ChatComposerHandle | null>;
Expand All @@ -224,6 +228,8 @@ export function useQueuedRunEdit(input: {
routeThreadRef,
threadDraftTarget,
activeThreadId,
ownsEdit,
recoveryTarget,
serverProjection,
promptRef,
composerRef,
Expand All @@ -232,8 +238,12 @@ export function useQueuedRunEdit(input: {
const promoteQueuedRun = useAtomCommand(threadEnvironment.promoteQueuedRun);
const editKey = scopedThreadKey(routeThreadRef);
const storedEdit = useAtomValue(queuedRunEdits.atom)[editKey] ?? null;
const editing = activeThreadId === null ? null : storedEdit;
const isSaving = useAtomValue(queuedRunEdits.savingAtom)[editKey] === true;
const editing =
activeThreadId === null || (storedEdit !== null && ownsEdit?.(storedEdit.context) === false)
? null
: storedEdit;
const saveInFlight = useAtomValue(queuedRunEdits.savingAtom)[editKey] === true;
const isSaving = editing !== null && saveInFlight;
const draftTarget =
editing === null ? threadDraftTarget : queuedRunEditDraftId(routeThreadRef, editing.runId);

Expand Down Expand Up @@ -279,15 +289,15 @@ export function useQueuedRunEdit(input: {
const clearEditDraft = (runId: RunId) => {
const target = queuedRunEditDraftId(routeThreadRef, runId);
// A save in flight still sends these uploads.
if (isSaving) {
if (saveInFlight) {
const store = useComposerDraftStore.getState();
store.clearComposerContent(target);
store.clearDraftThread(target);
} else discardQueuedEditDraft(target);
};

const beginEdit = useStableCallback((request: EditQueuedRunRequest) => {
if (activeThreadId === null) return;
if (activeThreadId === null || (storedEdit !== null && editing === null)) return;
if (editing !== null && editing.runId !== request.runId) clearEditDraft(editing.runId);
const target = queuedRunEditDraftId(routeThreadRef, request.runId);
const sentText = stripAttachmentOnlyBootstrapPrompt(request.text);
Expand Down Expand Up @@ -350,7 +360,7 @@ export function useQueuedRunEdit(input: {
onRunLeftQueue: (_, edit) => {
const recovery = recoverQueuedMessageEdit({
editTarget: queuedRunEditDraftId(routeThreadRef, edit.runId),
threadTarget: threadDraftTarget,
threadTarget: recoveryTarget?.(edit.context) ?? threadDraftTarget,
originalText: edit.originalText,
...(edit.originalModelSelection
? { originalModelSelection: edit.originalModelSelection }
Expand Down Expand Up @@ -387,6 +397,7 @@ export function useQueuedRunEdit(input: {
);
focusComposerSoon();
});
const ownsEditNow = useStableCallback(() => editing !== null);
const cancelEdit = useStableCallback(() => {
if (editing === null) return;
clearEditDraft(editing.runId);
Expand Down Expand Up @@ -541,10 +552,12 @@ export function useQueuedRunEdit(input: {
if (error !== null) return;
if (changedAfterSave) return;
releaseDraftAttachments(newAttachments);
promptRef.current = "";
clearEditDraft(editing.runId);
composerRef.current?.resetCursorState();
focusComposerSoon();
if (ownsEditNow()) {
promptRef.current = "";
composerRef.current?.resetCursorState();
focusComposerSoon();
}
// A plain submit means "Update queued message"; only the opposite-action shortcut or
// modifier-click asks to steer, as it does for a new follow-up.
if (submission?.submissionIntent === "alternate" && submission.dispatchMode === "steer") {
Expand All @@ -555,6 +568,7 @@ export function useQueuedRunEdit(input: {
return {
editingRunId: editing?.runId ?? null,
isEditing: editing !== null,
isEditingElsewhere: storedEdit !== null && editing === null,
isSaving,
draftTarget,
editingAttachments,
Expand Down
Loading
Loading