fix(webui): recognize typed session handles as references (#6311)
* fix(webui): recognize typed session handles as references Refs NAN-286 * refactor(webui): simplify session reference handling * fix(webui): recognize mentions after punctuation * docs(webui): focus topic references on usage
This commit is contained in:
1 parent
1f8e49c70a
commit
909b277e7a
7 files changed
+369
-144
No files matched your search
@@ -45,7 +45,11 @@ import {
|
||||
mcpPresetInitials,
|
||||
splitCapabilityMentionSegments,
|
||||
} from "@/components/CliAppMentionText";
|
||||
import type { CapabilityMentionSegment } from "@/lib/composer-mention-text";
|
||||
import {
|
||||
composerMentionText,
|
||||
isCapabilityMentionStart,
|
||||
type CapabilityMentionSegment,
|
||||
} from "@/lib/composer-mention-text";
|
||||
import { INLINE_TOKEN_HIGHLIGHT_COLOR } from "@/components/InlineTokenHighlight";
|
||||
import {
|
||||
ArrowUp,
|
||||
@@ -138,7 +142,6 @@ import {
|
||||
} from "@/lib/session-drag";
|
||||
import { formatQuotedUserMessage } from "@/lib/user-message-quote";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { composerMentionText } from "@/lib/composer-mention-text";
|
||||
import type { ComposerDraftStore } from "@/lib/composer-draft";
|
||||
|
||||
const VOICE_SHORTCUT_CODE = "KeyD";
|
||||
@@ -430,7 +433,7 @@ function mentionInsertion(
|
||||
const to = Math.min(Math.max(end, from), value.length);
|
||||
const prefix = value.slice(0, from);
|
||||
const suffix = value.slice(to);
|
||||
const leadingSpace = prefix && !/\s$/.test(prefix) ? " " : "";
|
||||
const leadingSpace = prefix && !isCapabilityMentionStart(value, from) ? " " : "";
|
||||
const trailingSpace = /^\s/.test(suffix) ? "" : " ";
|
||||
const tokenStart = prefix.length + leadingSpace.length;
|
||||
const tokenEnd = tokenStart + name.length + 1;
|
||||
@@ -1079,7 +1082,7 @@ export function ThreadComposer({
|
||||
const [value, setValue] = useState(initialDraft?.text ?? "");
|
||||
const [composerFocused, setComposerFocused] = useState(false);
|
||||
const blurFrame = useRef<number | null>(null);
|
||||
const [selectedSessionMentions, setSelectedSessionMentions] = useState<SessionMention[]>(
|
||||
const [boundSessionMentions, setBoundSessionMentions] = useState<SessionMention[]>(
|
||||
initialDraft?.sessionMentions ?? [],
|
||||
);
|
||||
const [sessionDragPreview, setSessionDragPreview] = useState<{
|
||||
@@ -1447,12 +1450,12 @@ export function ThreadComposer({
|
||||
if (interactionDisabled || cliAppMenuDismissed) return null;
|
||||
const caret = Math.min(Math.max(cursorPosition, 0), value.length);
|
||||
const beforeCaret = value.slice(0, caret);
|
||||
const match = /(?:^|\s)@([\p{L}\p{N}_-]*)$/iu.exec(beforeCaret);
|
||||
if (!match) return null;
|
||||
const match = /@([\p{L}\p{N}_-]*)$/iu.exec(beforeCaret);
|
||||
if (!match || !isCapabilityMentionStart(value, match.index)) return null;
|
||||
const query = match[1].toLowerCase();
|
||||
return {
|
||||
query,
|
||||
start: caret - query.length - 1,
|
||||
start: match.index,
|
||||
end: caret,
|
||||
};
|
||||
}, [cliAppMenuDismissed, cursorPosition, interactionDisabled, value]);
|
||||
@@ -1466,9 +1469,13 @@ export function ThreadComposer({
|
||||
() => sessionMentionOptions(sessions),
|
||||
[sessions],
|
||||
);
|
||||
const sessionMentionCatalog = useMemo(
|
||||
() => [...availableSessionMentions, ...boundSessionMentions],
|
||||
[availableSessionMentions, boundSessionMentions],
|
||||
);
|
||||
const mentionSegments = useMemo(
|
||||
() => splitCapabilityMentionSegments(value, cliApps, mcpPresets, selectedSessionMentions),
|
||||
[cliApps, mcpPresets, selectedSessionMentions, value],
|
||||
() => splitCapabilityMentionSegments(value, cliApps, mcpPresets, sessionMentionCatalog),
|
||||
[cliApps, mcpPresets, sessionMentionCatalog, value],
|
||||
);
|
||||
const editMentionInput = useCallback((next: string, cursor: number) => {
|
||||
secondEnterPromptIdRef.current = null;
|
||||
@@ -1485,19 +1492,6 @@ export function ThreadComposer({
|
||||
});
|
||||
const { rawSelection, replace: replaceMentionInput } = mentionInput;
|
||||
const draftText = composerMentionText(mentionInput.segments).raw;
|
||||
useLayoutEffect(() => {
|
||||
if (!draftKey || !draftStore || !draftAttachmentsReady) return;
|
||||
if (!draftText && images.length === 0 && !quotedContext) {
|
||||
draftStore.delete(draftKey);
|
||||
return;
|
||||
}
|
||||
draftStore.set(draftKey, {
|
||||
text: draftText,
|
||||
files: images.map((image) => image.file),
|
||||
sessionMentions: selectedSessionMentions,
|
||||
quotedContext,
|
||||
}, persistDraft);
|
||||
}, [draftAttachmentsReady, draftKey, draftStore, images, persistDraft, quotedContext, selectedSessionMentions, draftText]);
|
||||
const sessionDragInsertion = sessionDragPreview
|
||||
? mentionInsertion(
|
||||
value,
|
||||
@@ -1511,7 +1505,7 @@ export function ThreadComposer({
|
||||
sessionDragInsertion.value,
|
||||
cliApps,
|
||||
mcpPresets,
|
||||
[...selectedSessionMentions, sessionDragPreview.mention],
|
||||
sessionMentionCatalog,
|
||||
)
|
||||
: mentionInput.segments;
|
||||
const activeSessionMentions = useMemo(() => {
|
||||
@@ -1522,15 +1516,37 @@ export function ThreadComposer({
|
||||
return [segment.mention];
|
||||
});
|
||||
}, [mentionSegments]);
|
||||
useLayoutEffect(() => {
|
||||
// Pin resolved identities so a catalog refresh cannot retarget an existing reference.
|
||||
if (activeSessionMentions.length !== boundSessionMentions.length
|
||||
|| activeSessionMentions.some((mention, index) => mention !== boundSessionMentions[index])) {
|
||||
setBoundSessionMentions(activeSessionMentions);
|
||||
}
|
||||
}, [activeSessionMentions, boundSessionMentions]);
|
||||
useLayoutEffect(() => {
|
||||
if (!draftKey || !draftStore || !draftAttachmentsReady) return;
|
||||
if (!draftText && images.length === 0 && !quotedContext) {
|
||||
draftStore.delete(draftKey);
|
||||
return;
|
||||
}
|
||||
draftStore.set(draftKey, {
|
||||
text: draftText,
|
||||
files: images.map((image) => image.file),
|
||||
sessionMentions: activeSessionMentions,
|
||||
quotedContext,
|
||||
}, persistDraft);
|
||||
}, [activeSessionMentions, draftAttachmentsReady, draftKey, draftStore, images, persistDraft, quotedContext, draftText]);
|
||||
const insertableSessionMentions = useMemo(() => availableSessionMentions.filter((mention) => {
|
||||
const bound = activeSessionMentions.find(
|
||||
(active) => active.name.toLowerCase() === mention.name.toLowerCase(),
|
||||
);
|
||||
return (!bound || bound.session_key === mention.session_key)
|
||||
&& (activeSessionMentions.length < SESSION_MENTIONS_LIMIT
|
||||
|| activeSessionMentions.some((active) => active.session_key === mention.session_key));
|
||||
}), [activeSessionMentions, availableSessionMentions]);
|
||||
const filteredMentionCandidates = useMemo<MentionCandidate[]>(() => {
|
||||
if (!cliAppMention) return [];
|
||||
const sessionCandidates: MentionCandidate[] = availableSessionMentions
|
||||
.filter((mention) => (
|
||||
activeSessionMentions.length < SESSION_MENTIONS_LIMIT
|
||||
|| activeSessionMentions.some(
|
||||
(selected) => selected.session_key === mention.session_key,
|
||||
)
|
||||
))
|
||||
const sessionCandidates: MentionCandidate[] = insertableSessionMentions
|
||||
.filter((mention) => [
|
||||
mention.name,
|
||||
mention.title,
|
||||
@@ -1542,7 +1558,7 @@ export function ThreadComposer({
|
||||
mention,
|
||||
}));
|
||||
const sessionNames = new Set(
|
||||
availableSessionMentions.map((mention) => mention.name.toLowerCase()),
|
||||
sessionMentionCatalog.map((mention) => mention.name.toLowerCase()),
|
||||
);
|
||||
const cliCandidates: MentionCandidate[] = cliApps
|
||||
.filter((app) => app.installed)
|
||||
@@ -1603,7 +1619,7 @@ export function ThreadComposer({
|
||||
remaining -= extra;
|
||||
}
|
||||
return groups.flatMap(({ candidates }, index) => candidates.slice(0, counts[index]));
|
||||
}, [activeSessionMentions, availableSessionMentions, cliAppMention, cliApps, mcpPresets]);
|
||||
}, [cliAppMention, cliApps, insertableSessionMentions, mcpPresets, sessionMentionCatalog]);
|
||||
|
||||
const showCliAppMenu = filteredMentionCandidates.length > 0;
|
||||
const showAnyPalette = showSlashMenu || showCliAppMenu;
|
||||
@@ -1741,7 +1757,6 @@ export function ThreadComposer({
|
||||
previousPendingQueueKeyRef.current = pendingQueueKey;
|
||||
secondEnterPromptIdRef.current = null;
|
||||
setValue("");
|
||||
setSelectedSessionMentions([]);
|
||||
setInlineError(null);
|
||||
setSlashMenuDismissed(false);
|
||||
setCliAppMenuDismissed(false);
|
||||
@@ -1879,38 +1894,24 @@ export function ThreadComposer({
|
||||
[handleStop, isStreaming, onStop, recentSlashCommands, replaceMentionInput, resizeTextarea, skillQuery, value],
|
||||
);
|
||||
|
||||
const insertMentionCandidate = useCallback(
|
||||
(candidate: MentionCandidate, start: number, end: number) => {
|
||||
if (candidate.kind === "session") {
|
||||
const alreadySelected = activeSessionMentions.some(
|
||||
(mention) => mention.session_key === candidate.mention.session_key,
|
||||
);
|
||||
if (!alreadySelected && activeSessionMentions.length >= SESSION_MENTIONS_LIMIT) return;
|
||||
const name = candidate.name.toLowerCase();
|
||||
setSelectedSessionMentions([
|
||||
...activeSessionMentions.filter((mention) => (
|
||||
mention.name.toLowerCase() !== name
|
||||
&& mention.session_key !== candidate.mention.session_key
|
||||
)),
|
||||
candidate.mention,
|
||||
]);
|
||||
}
|
||||
const insertion = mentionInsertion(value, candidate.name, start, end);
|
||||
const insertMention = useCallback(
|
||||
(name: string, start: number, end: number) => {
|
||||
const insertion = mentionInsertion(value, name, start, end);
|
||||
replaceMentionInput(insertion.value, insertion.cursor);
|
||||
setCliAppMenuDismissed(true);
|
||||
setSlashMenuDismissed(false);
|
||||
setInlineError(null);
|
||||
resizeTextarea();
|
||||
},
|
||||
[activeSessionMentions, replaceMentionInput, resizeTextarea, value],
|
||||
[replaceMentionInput, resizeTextarea, value],
|
||||
);
|
||||
|
||||
const chooseMentionCandidate = useCallback(
|
||||
(candidate: MentionCandidate) => {
|
||||
if (!cliAppMention) return;
|
||||
insertMentionCandidate(candidate, cliAppMention.start, cliAppMention.end);
|
||||
insertMention(candidate.name, cliAppMention.start, cliAppMention.end);
|
||||
},
|
||||
[cliAppMention, insertMentionCandidate],
|
||||
[cliAppMention, insertMention],
|
||||
);
|
||||
|
||||
const handleSessionDrop = useCallback((event: React.DragEvent) => {
|
||||
@@ -1921,30 +1922,14 @@ export function ThreadComposer({
|
||||
setSessionDragPreview(null);
|
||||
if (interactionDisabled) return true;
|
||||
const sessionKey = readDraggedSession(event.dataTransfer);
|
||||
const mention = availableSessionMentions.find(
|
||||
const mention = insertableSessionMentions.find(
|
||||
(candidate) => candidate.session_key === (sessionKey ?? preview?.mention.session_key),
|
||||
);
|
||||
if (!mention) return true;
|
||||
const caret = preview?.start ?? rawSelection().start;
|
||||
insertMentionCandidate(
|
||||
{
|
||||
kind: "session",
|
||||
name: mention.name,
|
||||
displayName: mention.title || mention.name,
|
||||
mention,
|
||||
},
|
||||
caret,
|
||||
preview?.end ?? rawSelection().end,
|
||||
);
|
||||
insertMention(mention.name, caret, preview?.end ?? rawSelection().end);
|
||||
return true;
|
||||
}, [
|
||||
availableSessionMentions,
|
||||
insertMentionCandidate,
|
||||
interactionDisabled,
|
||||
rawSelection,
|
||||
sessionDragPreview,
|
||||
value.length,
|
||||
]);
|
||||
}, [insertableSessionMentions, insertMention, interactionDisabled, rawSelection, sessionDragPreview]);
|
||||
|
||||
const previewSessionDrop = useCallback((event: React.DragEvent) => {
|
||||
if (!hasDraggedSession(event.dataTransfer)) return false;
|
||||
@@ -1954,13 +1939,10 @@ export function ThreadComposer({
|
||||
return true;
|
||||
}
|
||||
const sessionKey = readDraggedSession(event.dataTransfer);
|
||||
const mention = availableSessionMentions.find(
|
||||
const mention = insertableSessionMentions.find(
|
||||
(candidate) => candidate.session_key === sessionKey,
|
||||
);
|
||||
const alreadySelected = mention && activeSessionMentions.some(
|
||||
(candidate) => candidate.session_key === mention.session_key,
|
||||
);
|
||||
if (!mention || (!alreadySelected && activeSessionMentions.length >= SESSION_MENTIONS_LIMIT)) {
|
||||
if (!mention) {
|
||||
event.dataTransfer.dropEffect = "none";
|
||||
setSessionDragPreview(null);
|
||||
return true;
|
||||
@@ -1976,7 +1958,7 @@ export function ThreadComposer({
|
||||
: { mention, start, end }
|
||||
));
|
||||
return true;
|
||||
}, [activeSessionMentions, availableSessionMentions, interactionDisabled, rawSelection]);
|
||||
}, [insertableSessionMentions, interactionDisabled, rawSelection]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!sessionDragPreview) return;
|
||||
@@ -1990,7 +1972,6 @@ export function ThreadComposer({
|
||||
|
||||
const clearComposerText = useCallback((restoreFocus = true) => {
|
||||
setValue("");
|
||||
setSelectedSessionMentions([]);
|
||||
setInlineError(null);
|
||||
setSlashMenuDismissed(false);
|
||||
setCliAppMenuDismissed(false);
|
||||
@@ -2056,7 +2037,7 @@ export function ThreadComposer({
|
||||
secondEnterPromptIdRef.current = null;
|
||||
setQueuedPrompt(null);
|
||||
replaceMentionInput(prompt.text, prompt.text.length);
|
||||
setSelectedSessionMentions(prompt.sessionMentions ?? []);
|
||||
setBoundSessionMentions(prompt.sessionMentions ?? []);
|
||||
setInlineError(null);
|
||||
setSlashMenuDismissed(false);
|
||||
setCliAppMenuDismissed(false);
|
||||
|
||||
Reference in new issue
Block a user