Skip to content

The agent that grows with you

fix(desktop): let a slash command be typed anywhere in the prompt

Brooklyn Nicholson58d805302a09
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-trigger.test.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-trigger.test.ts
new file mode 100644
index 00000000000..62a194da20d
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-trigger.test.ts
@@ -0,0 +1,104 @@
+import type { Unstable_TriggerAdapter, Unstable_TriggerItem } from '@assistant-ui/core'
+import { act, renderHook } from '@testing-library/react'
+import { createRef } from 'react'
+import { describe, expect, it, vi } from 'vitest'
+
+import { composerPlainText, renderComposerContents, RICH_INPUT_SLOT } from '../rich-editor'
+
+import { useComposerTrigger } from './use-composer-trigger'
+
+/** A live contentEditable seeded with `text`, caret parked at the end. */
+function mountEditor(text: string) {
+ const editor = document.createElement('div')
+ editor.dataset.slot = RICH_INPUT_SLOT
+ editor.contentEditable = 'true'
+ document.body.append(editor)
+ renderComposerContents(editor, text)
+
+ const range = document.createRange()
+ range.selectNodeContents(editor)
+ range.collapse(false)
+ const selection = window.getSelection()!
+ selection.removeAllRanges()
+ selection.addRange(range)
+
+ return editor
+}
+
+const item = (command: string): Unstable_TriggerItem => ({
+ id: command,
+ type: 'slash',
+ label: command.slice(1),
+ metadata: { command, display: command, meta: '', group: 'Skills', action: '', rawText: command }
+})
+
+function mountTrigger(editor: HTMLDivElement, items: Unstable_TriggerItem[]) {
+ const editorRef = createRef<HTMLDivElement>() as { current: HTMLDivElement | null }
+ editorRef.current = editor
+
+ const draftRef = { current: composerPlainText(editor) }
+
+ const adapter: Unstable_TriggerAdapter = {
+ categories: () => [],
+ categoryItems: () => [],
+ search: () => items
+ }
+
+ const setComposerText = vi.fn()
+
+ const hook = renderHook(() =>
+ useComposerTrigger({
+ at: { adapter: null, loading: false },
+ draftRef,
+ editorRef,
+ requestMainFocus: vi.fn(),
+ setComposerText,
+ slash: { adapter, loading: false }
+ })
+ )
+
+ return { draftRef, hook, setComposerText }
+}
+
+describe('useComposerTrigger — slash anywhere in the prompt', () => {
+ it('opens the completion list for a slash typed mid-message', () => {
+ const editor = mountEditor('please run /cle')
+ const { hook } = mountTrigger(editor, [item('/clean')])
+
+ act(() => hook.result.current.refreshTrigger())
+
+ expect(hook.result.current.trigger).toMatchObject({ kind: '/', inline: true, query: 'cle' })
+ expect(hook.result.current.triggerItems).toHaveLength(1)
+ })
+
+ it('inserts the picked skill inline and keeps the surrounding prose intact', () => {
+ const editor = mountEditor('please run /cle')
+ const { hook } = mountTrigger(editor, [item('/clean')])
+
+ act(() => hook.result.current.refreshTrigger())
+ act(() => hook.result.current.replaceTriggerWithChip(item('/clean')))
+
+ // The `/cle` the user typed is replaced by the full command; "please run"
+ // in front of it survives untouched.
+ expect(composerPlainText(editor)).toBe('please run /clean ')
+ })
+
+ it('still opens the list for a slash at the start of the prompt', () => {
+ const editor = mountEditor('/cle')
+ const { hook } = mountTrigger(editor, [item('/clean')])
+
+ act(() => hook.result.current.refreshTrigger())
+
+ expect(hook.result.current.trigger).toMatchObject({ kind: '/', query: 'cle' })
+ expect(hook.result.current.trigger?.inline).toBeUndefined()
+ })
+
+ it('leaves a mid-message file path alone', () => {
+ const editor = mountEditor('open src/foo/bar')
+ const { hook } = mountTrigger(editor, [item('/clean')])
+
+ act(() => hook.result.current.refreshTrigger())
+
+ expect(hook.result.current.trigger).toBeNull()
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-trigger.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-trigger.ts
index 20abc03309f..e4ac85bd770 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-trigger.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-trigger.ts
@@ -191,10 +191,13 @@ export function useComposerTrigger({
// Picking a bare arg-taking command (e.g. `/personality`) shouldn't commit
// it — expand to its options step so the popover shows the inline list, just
// as typing `/personality ` by hand would. A serialized value with a space is
- // already an arg pick (`/personality alice`), so it commits normally.
+ // already an arg pick (`/personality alice`), so it commits normally. An
+ // inline (mid-message) pick never expands: it's a reference inside prose, so
+ // there's no command invocation for the args to belong to.
const command = (item.metadata as { command?: string } | undefined)?.command ?? ''
- const expandsToArgs = trigger.kind === '/' && !serialized.includes(' ') && desktopSlashCommandTakesArgs(command)
+ const expandsToArgs =
+ trigger.kind === '/' && !trigger.inline && !serialized.includes(' ') && desktopSlashCommandTakesArgs(command)
const text = starter || serialized.endsWith(' ') ? serialized : `${serialized} `
const directive = !starter && serialized.match(/^@([^:]+):(.+)$/)
diff --git a/apps/desktop/src/app/chat/composer/text-utils.test.ts b/apps/desktop/src/app/chat/composer/text-utils.test.ts
index 6c6a20780f6..ca2ac803576 100644
--- a/apps/desktop/src/app/chat/composer/text-utils.test.ts
+++ b/apps/desktop/src/app/chat/composer/text-utils.test.ts
@@ -44,14 +44,25 @@ describe('detectTrigger', () => {
it('does not treat file-style paths as slash triggers', () => {
expect(detectTrigger('src/foo/bar')).toBeNull()
expect(detectTrigger('/path/to/file')).toBeNull()
+ // Mid-message paths stay excluded too: a path keeps going past the command
+ // token, so the trailing-anchored inline trigger never matches it.
+ expect(detectTrigger('check src/foo/bar')).toBeNull()
+ expect(detectTrigger('look at /usr/local/bin')).toBeNull()
+ expect(detectTrigger('and/or')).toBeNull()
})
- it('does not trigger slash popover mid-message', () => {
- expect(detectTrigger('hello /')).toBeNull()
- expect(detectTrigger('hello /skill')).toBeNull()
+ it('treats a mid-message slash as an inline reference', () => {
+ // Skills have to be reachable anywhere in a prompt, not just at position 0.
+ expect(detectTrigger('hello /')).toEqual({ kind: '/', inline: true, query: '', tokenLength: 1 })
+ expect(detectTrigger('hello /clean')).toEqual({ kind: '/', inline: true, query: 'clean', tokenLength: 6 })
+ expect(detectTrigger('text\n/skill')).toEqual({ kind: '/', inline: true, query: 'skill', tokenLength: 6 })
+ })
+
+ it('does not carry arg completion into an inline slash reference', () => {
+ // Only a position-0 slash is a real invocation, so `/personality alic`
+ // mid-message is prose — the trigger ends at the command token.
expect(detectTrigger('hello there /personality alic')).toBeNull()
- expect(detectTrigger('text\n/skill')).toBeNull()
- expect(detectTrigger('multi word message /')).toBeNull()
+ expect(detectTrigger('run /tools enable foo')).toBeNull()
})
it('still anchors at-mention triggers strictly at the token edge', () => {
diff --git a/apps/desktop/src/app/chat/composer/text-utils.ts b/apps/desktop/src/app/chat/composer/text-utils.ts
index b9b6adc07f1..8bc6663210e 100644
--- a/apps/desktop/src/app/chat/composer/text-utils.ts
+++ b/apps/desktop/src/app/chat/composer/text-utils.ts
@@ -1,22 +1,35 @@
import { DATA_IMAGE_URL_RE, dataUrlToBlob } from '@/lib/embedded-images'
export interface TriggerState {
+ /** True for a `/` typed mid-message — an inline skill/command reference in
+ * prose rather than a command invocation. Arg completion doesn't apply. */
+ inline?: boolean
kind: '@' | '/'
query: string
tokenLength: number
}
// `@` triggers stop at the first whitespace — `@file:path` and `@diff` are
-// single tokens. `/` triggers keep going so the popover stays live while the
-// user types args (`/personality alic` → arg completer suggests `alice`).
-// Restricting the slash command name to `[a-zA-Z][\w-]*` avoids matching file
-// paths like `src/foo/bar`.
+// single tokens. Restricting the slash command name to `[a-zA-Z][\w-]*` avoids
+// matching file paths like `src/foo/bar`.
//
-// Slash commands only execute at the beginning of a message, so the `/`
-// trigger is anchored strictly at position 0 — not after whitespace — to
-// avoid opening the popover mid-message (e.g. `hello /`).
+// `/` triggers fire in two shapes, because a slash means two different things
+// depending on where it sits:
+//
+// - At position 0 it's a COMMAND invocation the app executes (SLASH_COMMAND_RE
+// is `^`-anchored, and so is the backend's). The popover stays live past the
+// command name so arg completion works (`/personality alic` → `alice`).
+// - After whitespace it's an inline REFERENCE the user is dropping into prose
+// ("clean this up with /clean"). The text submits as an ordinary message, so
+// there are no args to complete — the trigger is a single token that ends at
+// the next space, exactly like `@`.
+//
+// The inline shape is what makes skills reachable anywhere in a prompt. Both
+// shapes need the trailing `$`: detection runs against the text BEFORE the
+// caret, so the match must end where the user is typing.
const AT_TRIGGER_RE = /(?:^|[\s])(@)([^\s@/]*)$/
-const SLASH_TRIGGER_RE = /^(\/)((?:[a-zA-Z][\w-]*(?:\s+\S*)*)?)$/
+const SLASH_COMMAND_TRIGGER_RE = /^(\/)((?:[a-zA-Z][\w-]*(?:\s+\S*)*)?)$/
+const SLASH_INLINE_TRIGGER_RE = /[\s](\/)([a-zA-Z][\w-]*)?$/
/** Stable key for paste dedupe — `items` and `files` often mirror the same image as different objects. */
export function blobDedupeKey(blob: Blob): string {
@@ -107,10 +120,20 @@ export function textBeforeCaret(editor: HTMLDivElement): string | null {
}
export function detectTrigger(textBefore: string): TriggerState | null {
- const slash = SLASH_TRIGGER_RE.exec(textBefore)
+ const command = SLASH_COMMAND_TRIGGER_RE.exec(textBefore)
+
+ if (command) {
+ return { kind: '/', query: command[2], tokenLength: 1 + command[2].length }
+ }
+
+ // An inline `/skill` is a reference dropped into prose, so it carries no args
+ // and the whole match is the token the chip replaces.
+ const inline = SLASH_INLINE_TRIGGER_RE.exec(textBefore)
+
+ if (inline) {
+ const query = inline[2] ?? ''
- if (slash) {
- return { kind: '/', query: slash[2], tokenLength: 1 + slash[2].length }
+ return { inline: true, kind: '/', query, tokenLength: 1 + query.length }
}
const at = AT_TRIGGER_RE.exec(textBefore)