Skip to content

The agent that grows with you

fix(desktop): don't let slash Space steal a leftover highlight

Brooklyn Nicholsone870d3fde328
diff --git a/apps/desktop/src/app/chat/composer/composer-utils.test.ts b/apps/desktop/src/app/chat/composer/composer-utils.test.ts
index 062b2567738..746115002cf 100644
--- a/apps/desktop/src/app/chat/composer/composer-utils.test.ts
+++ b/apps/desktop/src/app/chat/composer/composer-utils.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest'
import {
acceptsTriggerCompletion,
+ implicitSlashAcceptIndex,
isPendingDraftPersistCurrent,
type PendingDraftPersist,
pickPlaceholder,
@@ -88,6 +89,34 @@ describe('acceptsTriggerCompletion', () => {
})
})
+describe('implicitSlashAcceptIndex', () => {
+ const rows = ['/compress', '/review', '/resume']
+
+ it('completes a prefix of the highlighted row', () => {
+ expect(implicitSlashAcceptIndex('com', rows, 0, false)).toBe(0)
+ })
+
+ it('keeps a fully typed command even when another row is highlighted', () => {
+ expect(implicitSlashAcceptIndex('review', rows, 0, false)).toBe(1)
+ })
+
+ it('does not steal when the typed token is not a prefix of any row', () => {
+ expect(implicitSlashAcceptIndex('review', ['/compress', '/resume'], 0, false)).toBeNull()
+ })
+
+ it('takes the only prefix match when the highlight is a leftover', () => {
+ expect(implicitSlashAcceptIndex('rev', ['/compress', '/review', '/resume'], 0, false)).toBe(1)
+ })
+
+ it('honours an arrowed pick even when it is not a prefix', () => {
+ expect(implicitSlashAcceptIndex('review', rows, 0, true)).toBe(0)
+ })
+
+ it('matches an arg-stage prefix against the full completion text', () => {
+ expect(implicitSlashAcceptIndex('personality alic', ['/personality alice', '/personality none'], 0, false)).toBe(0)
+ })
+})
+
describe('pickPlaceholder', () => {
it('returns a member of the pool', () => {
const pool = ['a', 'b', 'c'] as const
diff --git a/apps/desktop/src/app/chat/composer/composer-utils.ts b/apps/desktop/src/app/chat/composer/composer-utils.ts
index 32728358928..65b141a0de7 100644
--- a/apps/desktop/src/app/chat/composer/composer-utils.ts
+++ b/apps/desktop/src/app/chat/composer/composer-utils.ts
@@ -87,6 +87,50 @@ export const slashArgStage = (query: string) => query.includes(' ')
/** The `/command` token of a slash query (`personality x` → `/personality`). */
export const slashCommandToken = (query: string) => `/${query.split(/\s+/, 1)[0]?.toLowerCase() ?? ''}`
+/** Typed `/` query or completion text without the leading slash. */
+export const slashCompletionToken = (value: string) => value.replace(/^\//, '').trimEnd().toLowerCase()
+
+/**
+ * Which row Space/Enter should take. Tab always uses the highlight; this is
+ * the "still suggesting, don't steal what I typed" pick.
+ *
+ * `/com` + a highlighted `/compress` still completes. `/review` while
+ * `/compress` is the leftover highlight does not. An exact list match wins
+ * so a fully typed command isn't replaced by a longer/fuzzier neighbour.
+ */
+export function implicitSlashAcceptIndex(
+ query: string,
+ itemTexts: readonly string[],
+ activeIndex: number,
+ activeExplicit: boolean
+): number | null {
+ const typed = slashCompletionToken(query)
+
+ if (!typed) {
+ return null
+ }
+
+ if (activeExplicit && itemTexts[activeIndex] != null) {
+ return activeIndex
+ }
+
+ const exact = itemTexts.findIndex(text => slashCompletionToken(text) === typed)
+
+ if (exact >= 0) {
+ return exact
+ }
+
+ const active = itemTexts[activeIndex]
+
+ if (active != null && slashCompletionToken(active).startsWith(typed)) {
+ return activeIndex
+ }
+
+ const prefixHits = itemTexts.flatMap((text, index) => (slashCompletionToken(text).startsWith(typed) ? [index] : []))
+
+ return prefixHits.length === 1 ? prefixHits[0] : null
+}
+
export interface TriggerAcceptInput {
/** The user moved the highlight themselves (arrow keys) rather than
* inheriting the list's default first row. */
diff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsx
index 2b2e21f0986..35e9fcfb373 100644
--- a/apps/desktop/src/app/chat/composer/index.tsx
+++ b/apps/desktop/src/app/chat/composer/index.tsx
@@ -31,6 +31,7 @@ import { AttachmentList } from './attachments'
import {
acceptsTriggerCompletion,
COMPOSER_FADE_BACKGROUND,
+ implicitSlashAcceptIndex,
type QueueEditState,
slashArgStage
} from './composer-utils'
@@ -694,9 +695,11 @@ export function ChatBar({
return
}
- // Accepting the highlighted item: a no-arg command commits its directive
- // chip, an arg-taking command expands to its options step, and an arg
- // option commits the full `/cmd arg` chip.
+ // Accepting a completion: a no-arg command commits its directive chip,
+ // an arg-taking command expands to its options step, and an arg option
+ // commits the full `/cmd arg` chip. Space/Enter resolve the pick so a
+ // leftover highlight does not replace a typed command; Tab still takes
+ // the highlight.
const accept = acceptsTriggerCompletion({
activeExplicit: triggerActiveExplicit,
freeTextArgStage: slashFreeTextArgStage,
@@ -706,17 +709,34 @@ export function ChatBar({
})
if (accept) {
- event.preventDefault()
- triggerKeyConsumedRef.current = true
- const item = triggerItems[triggerActive]
+ const itemTexts = triggerItems.map(item => {
+ const meta = item.metadata as { command?: string; rawText?: string } | undefined
+
+ return meta?.command || meta?.rawText || item.label
+ })
+
+ const item =
+ trigger.kind === '/' && event.key !== 'Tab'
+ ? triggerItems[
+ implicitSlashAcceptIndex(trigger.query, itemTexts, triggerActive, triggerActiveExplicit) ?? -1
+ ]
+ : triggerItems[triggerActive]
if (item) {
+ event.preventDefault()
+ triggerKeyConsumedRef.current = true
// Tab means "go deeper" on a folder; Enter means "I want this one".
- // Everything else treats them alike.
replaceTriggerWithChip(item, { descend: event.key === 'Tab' })
+
+ return
}
- return
+ if (event.key === 'Tab') {
+ event.preventDefault()
+ triggerKeyConsumedRef.current = true
+
+ return
+ }
}
// Backspace climbs out of an `@` path one segment at a time, mirroring