feat(web): prompt stash — cmd+S saves the composer to a per-provider queue (#4453)
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
f4c394323d
commit
200fa826b0
11 files changed
+1784
-1
No files matched your search
@@ -51,13 +51,29 @@ import {
|
||||
type ComposerImageAttachment,
|
||||
type DraftId,
|
||||
type PersistedComposerImageAttachment,
|
||||
hydrateImagesFromPersisted,
|
||||
useComposerDraftStore,
|
||||
useComposerThreadDraft,
|
||||
useEffectiveComposerModelState,
|
||||
} from "../../composerDraftStore";
|
||||
import {
|
||||
EMPTY_PROMPT_STASH_QUEUE,
|
||||
MAX_STASH_ENTRIES_PER_QUEUE,
|
||||
partitionStashAttachments,
|
||||
promptStashScopeKey,
|
||||
usePromptStashStore,
|
||||
type PromptStashEntry,
|
||||
} from "../../promptStashStore";
|
||||
import { ComposerStashBadge } from "./ComposerStashBadge";
|
||||
import { ComposerStashMenu } from "./ComposerStashMenu";
|
||||
import { compressImageForStash } from "../../lib/stashImageCompression";
|
||||
import { isCommandPaletteOpen } from "../../commandPaletteBus";
|
||||
import { getTerminalFocusOwner } from "../../lib/terminalFocus";
|
||||
import { resolveShortcutCommand } from "../../keybindings";
|
||||
import {
|
||||
type TerminalContextDraft,
|
||||
type TerminalContextSelection,
|
||||
INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
|
||||
insertInlineTerminalContextPlaceholder,
|
||||
removeInlineTerminalContextPlaceholder,
|
||||
} from "../../lib/terminalContext";
|
||||
@@ -723,6 +739,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
const clearComposerDraftPersistedAttachments = useComposerDraftStore(
|
||||
(store) => store.clearPersistedAttachments,
|
||||
);
|
||||
const clearComposerDraftPromptAndImages = useComposerDraftStore(
|
||||
(store) => store.clearComposerPromptAndImages,
|
||||
);
|
||||
const setComposerDraftModelSelection = useComposerDraftStore((store) => store.setModelSelection);
|
||||
const syncComposerDraftPersistedAttachments = useComposerDraftStore(
|
||||
(store) => store.syncPersistedAttachments,
|
||||
);
|
||||
@@ -961,6 +981,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
const [isComposerModelPickerOpen, setIsComposerModelPickerOpen] = useState(false);
|
||||
const [isComposerFocused, setIsComposerFocused] = useState(false);
|
||||
const [composerMenuAnchor, setComposerMenuAnchor] = useState<HTMLDivElement | null>(null);
|
||||
const [isStashMenuOpen, setIsStashMenuOpen] = useState(false);
|
||||
const [stashPulse, setStashPulse] = useState<{ key: number; active: boolean }>({
|
||||
key: 0,
|
||||
active: false,
|
||||
});
|
||||
const isMobileViewport = useMediaQuery("max-sm");
|
||||
const isComposerCollapsedMobile =
|
||||
isMobileViewport && !forceExpandedOnMobile && !isComposerFocused;
|
||||
@@ -980,6 +1005,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
const mobileComposerExpandReleaseFrameRef = useRef<number | null>(null);
|
||||
const mobileComposerExpandInFlightRef = useRef(false);
|
||||
const dragDepthRef = useRef(0);
|
||||
const stashPulseKeyRef = useRef(0);
|
||||
const stashPulseTimeoutRef = useRef<number | null>(null);
|
||||
/**
|
||||
* Snapshots currently being encoded, keyed by target+prompt+image ids.
|
||||
* Keyed rather than boolean so a genuinely different prompt (or a different
|
||||
* thread) can still be stashed while an earlier encode is running.
|
||||
*/
|
||||
const stashInFlightRef = useRef<Set<string>>(new Set());
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Derived: composer send state
|
||||
@@ -1861,6 +1894,400 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
return false;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Prompt stash (⌘S)
|
||||
// ------------------------------------------------------------------
|
||||
const stashScopeInstanceId = noProviderAvailable ? null : selectedInstanceId;
|
||||
const stashScope = promptStashScopeKey(stashScopeInstanceId);
|
||||
const stashQueue = usePromptStashStore(
|
||||
(state) => state.queuesByScopeKey[stashScope] ?? EMPTY_PROMPT_STASH_QUEUE,
|
||||
);
|
||||
const stashOtherScopesCount = usePromptStashStore((state) =>
|
||||
Object.entries(state.queuesByScopeKey).reduce(
|
||||
(total, [key, queue]) => (key === stashScope ? total : total + queue.length),
|
||||
0,
|
||||
),
|
||||
);
|
||||
const stashEntryToQueue = usePromptStashStore((state) => state.stashEntry);
|
||||
const takeStashEntry = usePromptStashStore((state) => state.takeEntry);
|
||||
const finalizeStashEntryImages = usePromptStashStore((state) => state.finalizeEntryImages);
|
||||
const stashProviderLabel = noProviderAvailable
|
||||
? "No provider"
|
||||
: getProviderDisplayName(providerStatuses, selectedProvider);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (stashPulseTimeoutRef.current !== null) {
|
||||
window.clearTimeout(stashPulseTimeoutRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
/** Briefly highlight the badge so the save registers without a flourish. */
|
||||
const pulseStashBadge = useCallback(() => {
|
||||
stashPulseKeyRef.current += 1;
|
||||
setStashPulse({ key: stashPulseKeyRef.current, active: true });
|
||||
if (stashPulseTimeoutRef.current !== null) {
|
||||
window.clearTimeout(stashPulseTimeoutRef.current);
|
||||
}
|
||||
stashPulseTimeoutRef.current = window.setTimeout(() => {
|
||||
stashPulseTimeoutRef.current = null;
|
||||
setStashPulse((current) => ({ ...current, active: false }));
|
||||
}, 1200);
|
||||
}, []);
|
||||
|
||||
const restoreStashEntry = useCallback(
|
||||
(entry: PromptStashEntry) => {
|
||||
// Remove first so a double activation (click + Enter) can't restore twice.
|
||||
const { entry: taken, durable } = takeStashEntry(
|
||||
promptStashScopeKey(entry.providerInstanceId),
|
||||
entry.id,
|
||||
);
|
||||
if (!taken) return;
|
||||
if (!durable) {
|
||||
toastManager.add({
|
||||
type: "warning",
|
||||
title: "Restored prompt may reappear in the stash",
|
||||
description:
|
||||
"Browser storage rejected the update, so this entry could still be there after a reload.",
|
||||
data: { hideCopyButton: true },
|
||||
});
|
||||
}
|
||||
setIsStashMenuOpen(false);
|
||||
|
||||
const currentPrompt = promptRef.current;
|
||||
// An image-only stash must not append blank lines to whatever is
|
||||
// already in the composer.
|
||||
const nextPrompt =
|
||||
entry.prompt.length === 0
|
||||
? currentPrompt
|
||||
: currentPrompt.trim().length
|
||||
? `${currentPrompt.replace(/\s+$/, "")}\n\n${entry.prompt}`
|
||||
: entry.prompt;
|
||||
const promptChanged = nextPrompt !== currentPrompt;
|
||||
if (promptChanged) {
|
||||
promptRef.current = nextPrompt;
|
||||
setComposerDraftPrompt(composerDraftTarget, nextPrompt);
|
||||
setComposerCursor(collapseExpandedComposerCursor(nextPrompt, nextPrompt.length));
|
||||
setComposerTrigger(null);
|
||||
}
|
||||
|
||||
let unrestoredImageNames: string[] = [];
|
||||
if (entry.attachments.length > 0) {
|
||||
const existingIds = new Set(composerImagesRef.current.map((image) => image.id));
|
||||
// The draft store also dedupes by mimeType+sizeBytes+name, so filter
|
||||
// on the same key here. Counting a duplicate against capacity would
|
||||
// burn a slot the store then refuses to fill, pushing a genuinely
|
||||
// unique image into the overflow list for nothing.
|
||||
const existingDedupKeys = new Set(
|
||||
composerImagesRef.current.map(
|
||||
(image) => `${image.mimeType} | ||||