fix(desktop): chip a bare @path into the ref it means

Tab-descending into a folder from the @ popover re-types the token as a
bare `@apps/desktop/` so the next complete.path lists its children. That
is right while typing, but a bare token is not a reference —
REFERENCE_PATTERN only matches @kind:value — so a draft sent with one
rendered as plain text and attached nothing at all.

Promote it to @file:/@folder: on the way out, the same shape url-refs.ts
uses for bare links: on the committing space, on paste, and on submit for
a path that never got its space. A '/' is required so a handle like
@teknium1 is never mistaken for a path.
This commit is contained in:
Brooklyn Nicholson 2026-07-27 23:13:32 -05:00
parent 73f8ddbb8b
commit 90797eb224
5 changed files with 235 additions and 4 deletions

View file

@ -9,6 +9,7 @@ import { enqueueQueuedPrompt, type QueuedPromptEntry } from '@/store/composer-qu
import { cloneAttachments, type QueueEditState } from '../composer-utils'
import { onComposerSubmitRequest } from '../focus'
import { pathifyRefs } from '../path-refs'
import { composerPlainText } from '../rich-editor'
import { useComposerScope } from '../scope'
import type { ChatBarProps } from '../types'
@ -139,7 +140,10 @@ export function useComposerSubmit({
}
}
const text = draftRef.current
// A path that never got its committing space (`@apps/desktop/` left by a Tab
// descend, then Enter) is still the reference the user picked — promote it
// on the way out so it attaches instead of submitting as inert text.
const text = pathifyRefs(draftRef.current)
const payloadPresent = text.trim().length > 0 || attachments.length > 0
// A clarify card parked on this session owns the turn: the agent is blocked

View file

@ -50,6 +50,7 @@ import { useComposerUrlDialog } from './hooks/use-composer-url-dialog'
import { useComposerVoice } from './hooks/use-composer-voice'
import { useSlashCompletions } from './hooks/use-slash-completions'
import { useSessionStatusPresence } from './hooks/use-status-presence'
import { chipTypedPathOnSpace, pathifyRefs } from './path-refs'
import { QueuePanel } from './queue-panel'
import {
composerPlainText,
@ -449,9 +450,9 @@ export function ChatBar({
// Links in the paste land as `@url:` chips rather than a wall of URL text —
// the same reference the "Add URL" dialog inserts, parsed in place so a link
// mid-sentence keeps its position.
// mid-sentence keeps its position. Bare `@path` tokens promote the same way.
recordUndoPoint()
insertComposerContentsAtCaret(event.currentTarget, linkifyUrls(pastedText))
insertComposerContentsAtCaret(event.currentTarget, pathifyRefs(linkifyUrls(pastedText)))
scheduleFlushEditorToDraft(event.currentTarget)
}
@ -519,6 +520,16 @@ export function ChatBar({
return
}
// Same for a bare `@path` — a hand-typed or Tab-descended path chips into
// the `@file:`/`@folder:` ref it means, instead of submitting as plain text
// the backend never resolves.
if (withUndoPoint(() => chipTypedPathOnSpace(event))) {
event.preventDefault()
flushEditorToDraft(event.currentTarget)
return
}
// Cmd/Ctrl+Shift+K drains the next queued message. Plain Cmd/Ctrl+K is
// reserved for the global command palette.
if ((event.metaKey || event.ctrlKey) && !event.altKey && event.shiftKey && event.key.toLowerCase() === 'k') {

View file

@ -0,0 +1,103 @@
import type { KeyboardEvent } from 'react'
import { describe, expect, it } from 'vitest'
import { chipTypedPathOnSpace, pathifyRefs } from './path-refs'
import { composerPlainText, RICH_INPUT_SLOT } from './rich-editor'
/**
* Tab-descending into a folder from the `@` popover leaves a bare
* `@apps/desktop/` in the editor. That is not a reference the backend's
* REFERENCE_PATTERN only matches `@kind:value` so it used to submit as plain
* text and silently attach nothing.
*/
/** An editor holding `text` with a collapsed caret at its end, plus the space
* keydown the composer would hand `chipTypedPathOnSpace`. */
const spaceOn = (text: string) => {
const editor = document.createElement('div')
editor.dataset.slot = RICH_INPUT_SLOT
editor.textContent = text
document.body.append(editor)
const selection = window.getSelection()!
const range = document.createRange()
range.setStart(editor.firstChild!, text.length)
range.collapse(true)
selection.removeAllRanges()
selection.addRange(range)
return { editor, event: { currentTarget: editor, key: ' ' } as KeyboardEvent<HTMLDivElement> }
}
describe('pathifyRefs', () => {
it('promotes a Tab-descended folder token', () => {
expect(pathifyRefs('look at @apps/desktop/')).toBe('look at @folder:`apps/desktop`')
})
it('promotes a typed file path', () => {
expect(pathifyRefs('read @src/main.ts please')).toBe('read @file:`src/main.ts` please')
})
it('promotes every bare path in one message', () => {
expect(pathifyRefs('@apps/desktop/ and @src/main.ts')).toBe('@folder:`apps/desktop` and @file:`src/main.ts`')
})
it('leaves an already-typed directive alone', () => {
const text = 'look at @folder:`apps/desktop` ok'
expect(pathifyRefs(text)).toBe(text)
})
it('leaves a handle alone', () => {
expect(pathifyRefs('cc @teknium1 on this')).toBe('cc @teknium1 on this')
})
it('leaves simple refs alone', () => {
expect(pathifyRefs('check @diff and @staged')).toBe('check @diff and @staged')
})
it('leaves an email alone', () => {
expect(pathifyRefs('mail me at brooklyn@nous.dev/x')).toBe('mail me at brooklyn@nous.dev/x')
})
it('is a no-op without an @', () => {
expect(pathifyRefs('nothing here')).toBe('nothing here')
})
})
describe('chipTypedPathOnSpace', () => {
it('chips a Tab-descended folder token into a real @folder: chip', () => {
const { editor, event } = spaceOn('look at @apps/desktop/')
expect(chipTypedPathOnSpace(event)).toBe(true)
const chip = editor.querySelector('[data-ref-text]')
expect(chip?.getAttribute('data-ref-kind')).toBe('folder')
expect(chip?.getAttribute('data-ref-id')).toBe('apps/desktop')
expect(chip?.textContent).toContain('desktop')
expect(composerPlainText(editor)).toBe('look at @folder:`apps/desktop` ')
})
it('chips a typed file path', () => {
const { editor, event } = spaceOn('read @src/main.ts')
expect(chipTypedPathOnSpace(event)).toBe(true)
expect(editor.querySelector('[data-ref-kind="file"]')).not.toBeNull()
expect(composerPlainText(editor)).toBe('read @file:`src/main.ts` ')
})
it('leaves a handle alone', () => {
const { editor, event } = spaceOn('cc @teknium1')
expect(chipTypedPathOnSpace(event)).toBe(false)
expect(editor.querySelector('[data-ref-text]')).toBeNull()
})
it('leaves an already-typed ref alone', () => {
const { event } = spaceOn('look at @folder:`apps/desktop`')
expect(chipTypedPathOnSpace(event)).toBe(false)
})
})

View file

@ -0,0 +1,103 @@
/**
* Bare-path recognition for the composer. Walking into a folder from the `@`
* popover (Tab) re-types the token as a bare `@apps/desktop/` so the next
* `complete.path` lists that folder's children. That's right while typing, but
* a bare token is not a reference: `REFERENCE_PATTERN` only matches
* `@kind:value`, so a draft sent with one attaches nothing and renders as
* plain text instead of a chip.
*
* So the bare token is promoted to its typed form on the way out the same
* shape `url-refs.ts` uses for bare links.
*/
import type { KeyboardEvent } from 'react'
import { quoteRefValue, REF_RE, refChipElement, replaceBeforeCaret } from './rich-editor'
import { textBeforeCaret } from './text-utils'
// A `/` is required, exactly like URL_RE requires an explicit scheme: `@teknium1`
// and `@diff` are a handle and a simple ref, not paths, and guessing wrong turns
// someone's name into a file reference. A separator is the cheap signal that the
// user meant a path. The token also can't start with a `:` kind prefix — that is
// already a typed ref and REF_RE owns it.
const BARE_PATH_RE = /(?<![\w@/])@((?!(?:file|folder|url|image|tool|line|terminal|session|git):)[^\s@:]*\/[^\s@:]*)/g
const TYPED_BARE_PATH_RE = new RegExp(`${BARE_PATH_RE.source}$`)
/** Trailing `/` means a directory — that's how the gateway's completion emits
* folders, and what Tab-descending leaves behind. */
export function barePathRef(path: string) {
const trimmed = path.replace(/\/+$/, '')
return trimmed ? `@${path.endsWith('/') ? 'folder' : 'file'}:${quoteRefValue(trimmed)}` : null
}
/** Rewrite bare `@path` tokens as typed `@file:`/`@folder:` directives, leaving
* anything already inside a directive alone. Returns `text` unchanged when
* there are none. */
export function pathifyRefs(text: string) {
if (!text.includes('@')) {
return text
}
REF_RE.lastIndex = 0
const fenced = Array.from(text.matchAll(REF_RE)).map(match => {
const start = match.index ?? 0
return { end: start + match[0].length, start }
})
let out = ''
let cursor = 0
for (const match of text.matchAll(BARE_PATH_RE)) {
const start = match.index ?? 0
const ref = barePathRef(match[1] || '')
if (!ref || fenced.some(span => start >= span.start && start < span.end)) {
continue
}
out += `${text.slice(cursor, start)}${ref}`
cursor = start + match[0].length
}
return out + text.slice(cursor)
}
/** A plain space finishing a typed `@path` commits it as a chip, so a hand-typed
* path chips the same way a picked one does. Returns whether it ran, so a
* keydown handler can fall through on anything else. */
export function chipTypedPathOnSpace(event: KeyboardEvent<HTMLDivElement>) {
if (event.key !== ' ' || event.metaKey || event.ctrlKey || event.altKey) {
return false
}
const editor = event.currentTarget
// Runs on every space, so bail on the cheap native read before paying for the
// caret range walk (same guard shape as the trigger detector).
if (!editor.textContent?.includes('@')) {
return false
}
const before = textBeforeCaret(editor)
const match = before ? TYPED_BARE_PATH_RE.exec(before) : null
const token = match?.[0]
const ref = match?.[1] ? barePathRef(match[1]) : null
if (!token || !ref) {
return false
}
const directive = ref.match(/^@([^:]+):(.+)$/)
if (!directive) {
return false
}
const fragment = document.createDocumentFragment()
fragment.append(refChipElement(directive[1], directive[2]), document.createTextNode(' '))
return replaceBeforeCaret(editor, token.length, fragment)
}

View file

@ -30,6 +30,7 @@ import {
type InlineRefInput,
insertInlineRefsIntoEditor
} from '@/app/chat/composer/inline-refs'
import { chipTypedPathOnSpace, pathifyRefs } from '@/app/chat/composer/path-refs'
import {
composerPlainText,
insertComposerContentsAtCaret,
@ -543,7 +544,7 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
recordUndoPoint()
// Links land as `@url:` chips, same as the main composer.
insertComposerContentsAtCaret(event.currentTarget, linkifyUrls(pastedText))
insertComposerContentsAtCaret(event.currentTarget, pathifyRefs(linkifyUrls(pastedText)))
syncDraftFromEditor(event.currentTarget)
}
@ -685,6 +686,15 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
return
}
// Same for a bare `@path`.
if (withUndoPoint(() => chipTypedPathOnSpace(event))) {
event.preventDefault()
rememberInitialDraft()
syncDraftFromEditor(event.currentTarget)
return
}
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault()
submitEdit(event.currentTarget)