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 @@