From e8d7128fea197f1cdc4dcfb1af3bf33564197e8c Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Thu, 26 Nov 2026 21:25:36 +0100 Subject: [PATCH] ui: type /cwd and other slash commands anywhere in the chat input (#33070) * ui: type the working directory on a /cwd line in the chat input On a desktop, choosing the working directory from the + menu or the chip puts `/cwd ` above the message being written, with the caret after the path, instead of opening a separate search field. The suggestions follow what is typed there, Enter picks only from them, and closing the picker gives the message back as it was. The chat form's pickers float above the line being written, also in the collapsed form, and fit their suggestions between a min and a max width. A directory under home always shows from `~/`. Assisted-by: Claude (Opus 5.5 Medium) * ui: find working directories by a partial name, allowing for a typo A plain name on the /cwd line is now looked up anywhere under home, not only among its direct children, with the nearest folders listed first. A search that finds nothing is retried allowing for one wrong, missing, extra or swapped character. Assisted-by: Claude (Fable 5.2) * ui: keep the path pickers at one width and list hidden folders last The @ mention and /cwd pickers no longer resize with their results, and folders that are hidden or sit inside a hidden folder rank below regular ones unless the query itself starts with a dot. Assisted-by: Claude (Fable 5.2) * ui: list a relative path's own folder before its deeper namesakes A query with a path in it (ui/lib/voi) was matched only below some other folder, never at the top of the scope, so hidden copies were the only results. The path is now also followed from the scope itself and listed first. Assisted-by: Claude (Fable 5.2) * ui: fix the folder pickers' review findings - Read a backslash as a path separator under a Windows home or working directory, in the /cwd and @ pickers. - Leave Enter to an IME composition, and to the message when the caret is below the /cwd line. - Close the /cwd line before sending, so it is not sent as text. - Keep the highlighted folder when the deeper results arrive, and do not leave the deep search's failure unhandled. - Walk the typo search nearest depth first and stop once it finds something. - Surface the deep search's error when a relative path finds nothing. Assisted-by: Claude (Fable 5.2) * ui: highlight the first suggestion of every new round in the pickers A list that renders under a resting pointer no longer counts as a hover, and the folder picker starts each round of results on its first folder. Assisted-by: Claude (Fable 5.2) * ui: keep the new chat's form in place as chips appear under it Assisted-by: Claude (Fable 5.2) * ui: drop the command picker's icons and shorten /compact's description Assisted-by: Claude (Fable 5.2) * ui: always edit the system message in the chat form, without a model selector The + menu turns the form into the system message editor whether or not the conversation shows its system message, and the editor offers no model selector. Assisted-by: Claude (Fable 5.2) * ui: add /system-message and keep the form's size in system message mode The command turns the chat form into the system message editor. The editor's dashed edge is an inset outline in place of the card's own edge, so the card keeps its size. Assisted-by: Claude (Fable 5.2) * ui: find a rooted path's name further down, and retry a failed home lookup `@~/llama.cpp` and `/cwd ~/llama.cpp` list `~/git/llama.cpp` when home itself holds no such name. A failed lookup of the server's home directory is no longer kept for the session, so the working directory can show from `~/` once it works. Assisted-by: Claude (Fable 5.2) * ui: type slash commands anywhere in the message A command is found at the caret, after other words or on another line, not only at the start of the input. Picking one takes just the command out and leaves the words around it, and /cwd stays where it was typed (the + menu writes it at the caret), so the path no longer jumps to a line of its own above the message. Assisted-by: Claude (Opus 5.5) * ui: keep the words after an argless command, and track /cwd by its edits - only /prompt and /cwd read the words after them: Enter after `use /m the small one` no longer picks /model and deletes the sentence - the open /cwd picker follows its own command after an edit, never another /cwd the message names, and closes when its command is deleted - a blocked send leaves the /cwd command and its picker alone - callers of the server home share one request - a typo-tolerant folder search whose every glob fails shows the error - hidden folders sort last behind backslash separators too Assisted-by: Claude (Opus 5.5 Medium) --- .../app/chat/ChatForm/ChatForm.svelte | 86 ++++-- .../ChatFormCurrentWorkingDirectory.svelte | 143 ++++++---- ...mCurrentWorkingDirectoryResultsList.svelte | 6 +- .../ChatFormPickerListItem.svelte | 10 +- .../ChatFormPickerPopover.svelte | 3 +- .../ChatFormPickerCommand.svelte | 6 +- .../ChatFormPickerMention.svelte | 11 +- .../app/chat/ChatScreen/ChatScreen.svelte | 8 +- .../app/chat/ChatScreen/ChatScreenForm.svelte | 12 +- .../src/lib/constants/chat-form.constants.ts | 17 ++ .../chat/src/lib/constants/icons.constants.ts | 14 +- .../constants/working-directory.constants.ts | 24 +- ui/apps/chat/src/lib/enums/chat.enums.ts | 3 +- .../lib/hooks/use-chat-form-pickers.svelte.ts | 257 ++++++++++++----- ui/apps/chat/src/lib/stores/tools.svelte.ts | 45 +-- ui/apps/chat/src/lib/types/chat.d.ts | 4 + ui/apps/chat/src/lib/utils/chat-commands.ts | 17 +- ui/apps/chat/src/lib/utils/command-token.ts | 232 +++++++++++++++- ui/apps/chat/src/lib/utils/glob-search.ts | 262 ++++++++++++++++-- .../chat/src/lib/utils/has-pointer-moved.ts | 7 + ui/apps/chat/src/lib/utils/index.ts | 13 +- ui/apps/chat/src/lib/utils/path-display.ts | 13 +- .../chat/src/lib/utils/working-directory.ts | 101 ++++++- .../chat-form-slash-commands.svelte.test.ts | 214 +++++++++++++- .../components/ChatFormPickersHarness.svelte | 27 +- .../chat/tests/stories/chat/form/fixtures.ts | 2 +- ui/apps/chat/tests/unit/command-token.test.ts | 158 +++++++++-- ui/apps/chat/tests/unit/folder-search.test.ts | 142 ++++++++++ .../tests/unit/glob-search-children.test.ts | 146 ++++++++++ ui/apps/chat/tests/unit/tool-calls.test.ts | 16 ++ .../chat/tests/unit/working-directory.test.ts | 72 ++++- 31 files changed, 1774 insertions(+), 297 deletions(-) create mode 100644 ui/apps/chat/src/lib/utils/has-pointer-moved.ts create mode 100644 ui/apps/chat/tests/unit/folder-search.test.ts diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte index 6fa329d638..89cabf0c81 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatForm.svelte @@ -64,7 +64,6 @@ } from '#lib/types/index.ts'; import { buildMentionInsertion, - findCommandToken, findMentionToken, getConversationModel, isIMEComposing, @@ -140,6 +139,7 @@ let pickersRef: { handleKeydown: (event: KeyboardEvent) => boolean } | undefined = $state(undefined); let inputRef: ChatFormInput | undefined = $state(undefined); + let workingDirectoryRef: ChatFormCurrentWorkingDirectory | undefined = $state(undefined); // Voice input const recorder: VoiceRecorderLike = untrack(() => voiceRecorder) ?? new VoiceRecorder(); @@ -148,8 +148,8 @@ /** Why the last start failed (denied, no microphone, busy); the next tap tries again. */ let micError = $state(null); - // Invisible anchor the pickers' popovers float above: the form's top edge, or in the - // expanded editor the line being written + // Invisible anchor the pickers' popovers float above: the line being written, or on a + // phone the form's top edge let pickerAnchor: HTMLDivElement | null = $state(null); let pickerAnchorBox = $state({ height: 1, top: 0 }); @@ -174,6 +174,8 @@ conversationsStore.activeConversation ? systemMessage?.content : undefined ); + // a system message is the same whichever model answers: its editor offers no selector + let isModelSelectorShown = $derived(showModelSelector && !systemMode); let showWorkingDirectory = $derived(conversationsStore.preferences.hasEnabledCwdTools()); // On a desktop the chip under the form shows a directory once one is set, and the + menu // sets one until then. A phone has no chip: a chat shows it in its header (see @@ -185,12 +187,16 @@ const pickers = useChatFormPickers({ canCompact: () => chatStore.canCompact(conversationsStore.activeMessages), + canEditSystemMessage: () => !!onSystemPromptClick && !systemMode, + // a phone picks in a drawer with its own search field, and a touch screen opens no + // typed pickers (see opensOnInput) + editsCwdInline: () => !deviceStore.isMobile && !deviceStore.isTouch, focusInput: refocusInput, getCaretOffset: () => inputRef?.getCaretOffset(), getCwd: () => cwd, getPickersRef: () => pickersRef, getServerHome: () => toolsStore.serverHome ?? null, - getShowModelSelector: () => showModelSelector, + getShowModelSelector: () => isModelSelectorShown, getValue: () => value, hasCwdTools: () => conversationsStore.preferences.hasEnabledCwdTools(), // policy-aware, same rule as the agentic flow: MCP category on and at @@ -205,12 +211,17 @@ .some((s) => s.enabled && prefs.isServerToolsEnabled(s.id) && s.url.trim()); }, onCompact: () => chatStore.compactConversation(), + onSystemMessage: () => onSystemPromptClick?.({ files: uploadedFiles, message: value }), openModelSelector: () => chatFormActionsRef?.openModelSelector(), // TODO: no typed pickers with an on-screen keyboard for now: each mobile browser moves // the page its own way as it opens, so a list by the form can not stay clear of the // text. A drawer with its own search field (as the directory picker is) could. opensOnInput: () => !deviceStore.isTouch, - setCaretOffset: (offset) => inputRef?.setCaretOffset(offset), + setCaretOffset: (offset) => { + inputRef?.setCaretOffset(offset); + // the caret moved without typing, e.g. onto the `/cwd` line the + menu just added + measurePickerAnchor(); + }, setValue: (v) => { value = v; onValueChange?.(v); @@ -237,14 +248,9 @@ }); async function handleWorkingDirectoryChange(newDir: string | null) { - // Committing a directory consumes the `/cwd` token; the chip's - // clear-X path has no token to consume. - const token = findCommandToken(value); - - if (token && token.name === 'cwd') { - value = ''; - onValueChange?.(''); - } + // Committing a directory consumes the `/cwd` line and keeps the message below it; the + // chip's clear-X path has no line to consume. + pickers.consumeCwdCommand(); await conversationsStore.preferences.setCwd(newDir); @@ -365,18 +371,30 @@ isExpanded && deviceStore.isMobile ? (actionsEl ?? null) : pickerAnchor ); + // The anchor covers the caret's line, kept within the form when the line is scrolled out + // of the editor's view; on a phone the form's top edge + function measurePickerAnchor() { + const line = deviceStore.isMobile ? null : inputRef?.getCaretRect(); + const form = pickerAnchor?.parentElement?.getBoundingClientRect(); + + if (!line || !form) { + pickerAnchorBox = { height: 1, top: 0 }; + + return; + } + + const top = Math.min(Math.max(line.top - form.top, 0), form.height); + + pickerAnchorBox = { height: Math.max(line.bottom - line.top, 1), top }; + } + // before the popover is drawn, and again as the query is typed and the line moves $effect.pre(() => { if (!isPickerOpen) return; void value; - const line = isExpanded && !deviceStore.isMobile ? inputRef?.getCaretRect() : null; - const formTop = pickerAnchor?.parentElement?.getBoundingClientRect().top ?? 0; - - pickerAnchorBox = line - ? { height: line.bottom - line.top, top: line.top - formTop } - : { height: 1, top: 0 }; + measurePickerAnchor(); }); // shown on hover (always on touch screens) while collapsed, always while expanded: on // the phone sheet it keeps the collapsed card's place, below the status bar @@ -447,7 +465,14 @@ function handleKeydown(event: KeyboardEvent) { // Pickers consume navigation/escape keys first; when consumed, skip // the enter-to-submit logic below and keep the key from the editor. - if (pickers.handleKeydown(event)) { + // The directory picker takes the keys typed on its `/cwd` line; in the message below + // it only Escape, so Enter and the arrows there stay the message's + const isDirectoryKey = pickers.isCaretInCwdCommand() || event.key === KeyboardKey.ESCAPE; + + if ( + (isDirectoryKey && workingDirectoryRef?.handleKeydown(event)) || + pickers.handleKeydown(event) + ) { event.preventDefault(); return; @@ -678,6 +703,13 @@ async function submit() { if (!canSubmit || disabled || hasLoadingAttachments || isSendWaiting) return; + // sending closes the directory picker first, so its `/cwd` command is not sent as text + if (pickers.isWorkingDirectoryPickerOpen) { + pickers.handleWorkingDirectoryClose(); + + if (!canSubmit) return; + } + if (voice.isRefining) { isSendWaiting = true; @@ -809,7 +841,9 @@ 'max-md:border-transparent max-md:focus-within:border-transparent max-md:dark:border-transparent max-md:dark:focus-within:border-transparent', EXPANDED_SHEET_FILL ], - systemMode && '!border-2 !border-dashed !border-border/50 !bg-muted', + // a dashed outline stands in for the card's edge, drawn inside it: the card keeps its size + systemMode && + '!bg-muted !outline-2 -outline-offset-2 !outline-dashed !outline-border/50 [--glass-edge-focus:transparent] [--glass-edge:transparent]', disabled && 'cursor-not-allowed opacity-60' ]} data-single-row={isSingleRow || undefined} @@ -920,7 +954,7 @@ ? pickers.handleWorkingDirectoryOpen : undefined} {showAddButton} - {showModelSelector} + showModelSelector={isModelSelectorShown} voice={{ action: voice.label, hint: voice.hint, @@ -952,15 +986,21 @@ > {/if} -
+ +
{#if showWorkingDirectory} { - if (!isOpen) return; + if (!isOpen || inline) return; setTimeout(() => searchInputRef?.focus(), FOCUS_DELAY_MS); }); @@ -160,32 +167,14 @@ return; } - try { - // Generous limit: ranking is client-side, only the top - // MAX_RESULTS_SHOWN are shown. - // A plain query navigates home as if typed after `~/`; a query - // starting from a root or `~` navigates the absolute tree. - const res = await runGlobSearchWithChildren( - toHomeRelativeQuery(trimmed, homeBase), - homeBase ?? HOME_TILDE, - SEARCH.MAX_DEPTH, - SEARCH.LIMIT, - signal, - { type: GlobSearchType.DIR } - ); - + // Every round of results starts with its first folder highlighted. A stale + // search must not touch the list: the next one is already running. + const show = (res: GlobSearchChildResult) => { if (!isCurrent()) return; - if (res.error) { - queryResults = []; - nav.reset(-1); - searchError = res.error; - - return; - } - searchScope = res.exactDir ?? res.args.path; queryResults = res.entries.map((e) => e.path).slice(0, SEARCH.MAX_RESULTS_SHOWN); + searchError = res.error ?? null; if (queryResults.length > 0) { nav.reset(0); @@ -193,8 +182,10 @@ } else { nav.reset(-1); } + }; - searchError = null; + try { + show(await runFolderSearch(trimmed, homeBase, signal, show)); } catch (err) { if (!isCurrent() || signal.aborted) return; @@ -261,25 +252,60 @@ closePicker(); } - function handleKeydown(event: KeyboardEvent) { - if (event.key === KeyboardKey.ENTER) { + /** True when the key was the picker's: the chat input then leaves it alone. */ + function handlePickerKeydown(event: KeyboardEvent): boolean { + // a key that confirms or moves within an IME composition belongs to the text + if (isIMEComposing(event)) return false; + + // Enter picks a suggestion, never the typed text: before the results arrive it does + // nothing. Only without file search, where nothing is suggested, does it take the path + // as typed + if (event.key === KeyboardKey.ENTER && !event.shiftKey) { event.preventDefault(); if (nav.hoveredIndex >= 0 && queryResults[nav.hoveredIndex]) { commit(queryResults[nav.hoveredIndex]); - } else if (queryResults.length === 0) { + } else if (!fileSearchEnabled) { handleSubmit(); } - } else if (event.key === KeyboardKey.ARROW_DOWN) { - if (queryResults.length > 0) { - event.preventDefault(); - nav.move(1); - } - } else if (event.key === KeyboardKey.ARROW_UP) { - if (queryResults.length > 0) { - event.preventDefault(); - nav.move(-1); - } + + return true; + } + + if ( + (event.key === KeyboardKey.ARROW_DOWN || event.key === KeyboardKey.ARROW_UP) && + queryResults.length > 0 + ) { + event.preventDefault(); + nav.move(event.key === KeyboardKey.ARROW_DOWN ? 1 : -1); + + return true; + } + + if (inline && event.key === KeyboardKey.ESCAPE) { + event.preventDefault(); + closePicker(); + + return true; + } + + return false; + } + + /** The chat input's keys while the path is typed on its `/cwd` line. */ + export function handleKeydown(event: KeyboardEvent): boolean { + if (!isOpen || !inline) return false; + + return handlePickerKeydown(event); + } + + // Inline, the chat input is the picker's field: pressing in the form (to move the caret, + // say) keeps the picker open + function handleInteractOutside(event: PointerEvent) { + const target = event.target; + + if (inline && target instanceof Node && customAnchor?.closest('form')?.contains(target)) { + event.preventDefault(); } } @@ -380,14 +406,27 @@ {#snippet picker(inDrawer: boolean)} -
- + +
+ {#if inline} + {#if fileSearchEnabled && !query.trim()} +
+ Type a folder name or path after /cwd +
+ {/if} + {:else} + + {/if} {#if !fileSearchEnabled}
{searchUnavailableMessage}
@@ -460,7 +499,7 @@ {#if deviceStore.isMobile} - + Working directory @@ -484,16 +523,20 @@ Open working directory picker + event.preventDefault()} + onInteractOutside={handleInteractOutside} onOpenAutoFocus={(event) => event.preventDefault()} - onkeydown={handleKeydown} + onkeydown={handlePickerKeydown} preventScroll={false} side="top" sideOffset={12} + trapFocus={!inline} > {@render picker(false)} diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormCurrentWorkingDirectory/ChatFormCurrentWorkingDirectoryResultsList.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormCurrentWorkingDirectory/ChatFormCurrentWorkingDirectoryResultsList.svelte index bc368ac74a..659f9f62dc 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormCurrentWorkingDirectory/ChatFormCurrentWorkingDirectoryResultsList.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormCurrentWorkingDirectory/ChatFormCurrentWorkingDirectoryResultsList.svelte @@ -2,7 +2,7 @@ import { cn } from '@ggml-org/llama-ui/utils'; import { Folder } from '@lucide/svelte'; import { UI_DATA_ATTRS } from '#lib/constants/index.ts'; - import { highlightMatch } from '#lib/utils/index.ts'; + import { hasPointerMoved, highlightMatch } from '#lib/utils/index.ts'; import { fly } from 'svelte/transition'; // Fly-in transition for the results list. @@ -53,7 +53,9 @@ )} data-highlighted={index === hoveredIndex ? '' : undefined} onclick={() => onCommit?.(path)} - onmouseenter={() => onHover?.(index)} + onmousemove={(event) => { + if (hasPointerMoved(event)) onHover?.(index); + }} > diff --git a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormPickers/ChatFormPicker/ChatFormPickerListItem.svelte b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormPickers/ChatFormPicker/ChatFormPickerListItem.svelte index 03a76e61b5..180cb1fbc3 100644 --- a/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormPickers/ChatFormPicker/ChatFormPickerListItem.svelte +++ b/ui/apps/chat/src/lib/components/app/chat/ChatForm/ChatFormPickers/ChatFormPicker/ChatFormPickerListItem.svelte @@ -1,12 +1,14 @@