Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,60 @@ describe('deriveComposerAutocomplete — one grammar for every agent', () => {
expect(result.items.map((entry) => entry.token)).toEqual(['$typescript'])
})

it('no longer treats `$` as a composer trigger', () => {
expect(deriveComposerAutocomplete('use $type', 9, COMMANDS, skills, codex).mode).toBe('none')
expect(deriveComposerAutocomplete('$react', 6, COMMANDS, skills, codex).mode).toBe('none')
it('opens only Codex skills under `$`, and a pick keeps Codex `$` token', () => {
for (const [draft, query] of [
['use $type', 'type'],
['$', '']
] as const) {
const result = deriveComposerAutocomplete(draft, draft.length, COMMANDS, skills, codex)
expect(result).toMatchObject({ mode: 'slash', prefix: '$', query, dispatchable: false })
if (result.mode !== 'slash') {
return
}
expect(result.items.every((entry) => entry.kind === 'skill')).toBe(true)
}
const picked = applyPickerSuggestion('use $typ now', 8, {
kind: 'skill',
id: 'skill:typescript',
name: 'typescript',
token: '$typescript',
description: null,
sources: []
})
expect(picked.draft).toBe('use $typescript now')
})

it('leaves `$` as prose for agents whose skills use `/`', () => {
const claude = getNativeChatAgentProfile('claude')
expect(deriveComposerAutocomplete('use $type', 9, COMMANDS, skills, claude).mode).toBe('none')
})

it('does not fire inside shell-style text', () => {
expect(deriveComposerAutocomplete('price$tag', 9, COMMANDS, skills, codex).mode).toBe('none')
})

// `echo $HOME` + Enter once swapped `$HOME` for a skill whose description named $CODEX_HOME.
it.each(['echo $HOME', 'costs $5', 'run $(pwd)'])('leaves shell `$` in %j as prose', (draft) => {
expect(isSkillPickerTriggered(draft, codex)).toBe(false)
expect(deriveComposerAutocomplete(draft, draft.length, COMMANDS, skills, codex).mode).toBe(
'none'
)
})

it('matches `$` on skill names only, while `/` still searches descriptions', () => {
const installer = [
skill({
name: 'skill-installer',
description: 'Install Codex skills into $CODEX_HOME/skills'
})
]
const dollar = deriveComposerAutocomplete('echo $CODEX_HOME', 16, [], installer, codex)
expect(dollar.mode === 'slash' && dollar.items).toEqual([])
const slash = deriveComposerAutocomplete('use /codex_home', 15, [], installer, codex)
expect(slash.mode === 'slash' && slash.items.map((entry) => entry.name)).toEqual([
'skill-installer'
])
})
})

describe('filterSlashCommands', () => {
Expand Down Expand Up @@ -291,7 +337,7 @@ describe('native skill and command picker', () => {
expect(isSkillPickerTriggered('validate it with /elec', profile)).toBe(true)
expect(isSkillPickerTriggered('open /Users/me', profile)).toBe(false)
// Without a catalog fetch the menu would sit on a permanent loading row.
expect(isSkillPickerTriggered('use $elec', profile)).toBe(false)
expect(isSkillPickerTriggered('use $elec', profile)).toBe(agent === 'codex')
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
buildNativeChatPickerItems,
LEADING_SLASH_TRIGGER,
MID_PROMPT_SLASH_TRIGGER,
matchSkillSigil,
type NativeChatPickerItem,
type NativeChatSkillDiscoverySnapshot
} from '../../../../shared/native-chat-picker-items'
Expand All @@ -34,7 +35,8 @@ type PickerAutocomplete = {
query: string
items: NativeChatPickerItem[]
triggerKey: string
prefix: '/'
/** `$` lists only the skills of an agent whose skills carry that sigil. */
prefix: '/' | '$'
/** Only a draft-leading `/command` reaches the agent as a command. */
dispatchable: boolean
grouped: boolean
Expand All @@ -59,7 +61,11 @@ export function isSkillPickerTriggered(
if (!profile) {
return false
}
return LEADING_SLASH_TRIGGER.test(before) || MID_PROMPT_SLASH_TRIGGER.test(before)
return (
LEADING_SLASH_TRIGGER.test(before) ||
MID_PROMPT_SLASH_TRIGGER.test(before) ||
(profile.skillPrefix === '$' && matchSkillSigil(before) !== null)
)
}

export function deriveComposerAutocomplete(
Expand All @@ -76,6 +82,7 @@ export function deriveComposerAutocomplete(
const leadingMatch = before.match(LEADING_SLASH_TRIGGER)
if (leadingMatch) {
return deriveSlashAutocomplete(
'/',
leadingMatch[1],
0,
agentCommands,
Expand All @@ -92,17 +99,20 @@ export function deriveComposerAutocomplete(
? { mode: 'none' }
: { mode: 'mention', query: mentionMatch[1], triggerKey }
}
// Why: `/` is the whole composer grammar, so a mid-prompt token opens the same
// menu a leading one does — it just cannot dispatch.
const midPromptMatch = profile ? before.match(MID_PROMPT_SLASH_TRIGGER) : null
if (!midPromptMatch) {
// Why: `/` is the shared composer grammar, so a mid-prompt token opens the same
// menu a leading one does — it just cannot dispatch. `$` is the skill sigil's own
// skills-only menu.
const sigilQuery = profile?.skillPrefix === '$' ? matchSkillSigil(before) : null
const query = sigilQuery ?? (profile ? before.match(MID_PROMPT_SLASH_TRIGGER)?.[1] : undefined)
if (query === undefined) {
return { mode: 'none' }
}
const query = midPromptMatch[1]
const isSigil = sigilQuery !== null
return deriveSlashAutocomplete(
isSigil ? '$' : '/',
query,
before.length - query.length - 1,
agentCommands,
isSigil ? [] : agentCommands,
profile,
discovery,
dismissedTriggerKey,
Expand All @@ -111,6 +121,7 @@ export function deriveComposerAutocomplete(
}

function deriveSlashAutocomplete(
prefix: '/' | '$',
query: string,
triggerPosition: number,
agentCommands: readonly SlashCommandSuggestion[],
Expand All @@ -119,7 +130,7 @@ function deriveSlashAutocomplete(
dismissedTriggerKey: string | null,
sessionSkills: readonly NativeChatSessionSkill[] | undefined
): ComposerAutocomplete {
const triggerKey = `/:${triggerPosition}`
const triggerKey = `${prefix}:${triggerPosition}`
if (dismissedTriggerKey === triggerKey) {
return { mode: 'none' }
}
Expand All @@ -132,15 +143,17 @@ function deriveSlashAutocomplete(
skillsEnabled ? discovery.skills : [],
query,
profile?.skillPrefix ?? '/',
skillsEnabled ? sessionSkills : []
skillsEnabled ? sessionSkills : [],
undefined,
prefix
)
return {
mode: 'slash',
query,
triggerKey,
prefix: '/',
dispatchable: triggerPosition === 0,
grouped: skillsEnabled,
prefix,
dispatchable: prefix === '/' && triggerPosition === 0,
grouped: skillsEnabled && prefix === '/',
commandsEnabled: agentCommands.length > 0,
skillsEnabled,
items,
Expand Down
54 changes: 40 additions & 14 deletions src/shared/native-chat-picker-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export function buildNativeChatPickerItems(
skillSigil: '/' | '$',
sessionSkills?: readonly NativeChatSessionSkill[],
/** Rows kept per group; a caller that needs every skill passes Infinity. */
limit = PICKER_RESULT_LIMIT
limit = PICKER_RESULT_LIMIT,
/** `$` matches skill names only, so a shell word like `$CODEX_HOME` never hits a description. */
trigger: '/' | '$' = '/'
): NativeChatPickerItem[] {
// A name can only collide when both kinds invoke through the same sigil;
// where skills carry their own, `/review` and `$review` are distinct entries.
Expand Down Expand Up @@ -125,7 +127,8 @@ export function buildNativeChatPickerItems(
mergedSkills
.filter((skill) => !(sharedSigil && commandNames.has(skill.name)))
.map((item, index) => ({ item, stableOrder: index })),
query
query,
trigger === '/'
)
return [...commandItems.slice(0, limit), ...skillItems.slice(0, limit)]
}
Expand Down Expand Up @@ -200,21 +203,23 @@ function pickerSkill(

function rankItems<T extends NativeChatPickerItem>(
entries: { item: T; stableOrder: number }[],
query: string
query: string,
matchDescriptions = true
): T[] {
if (!query) {
return entries.map((entry) => entry.item)
}
return entries
.map((entry) => ({ ...entry, rank: getMatchRank(entry.item, query) }))
.map((entry) => ({ ...entry, rank: getMatchRank(entry.item, query, matchDescriptions) }))
.filter((entry) => entry.rank !== null)
.sort((a, b) => a.rank! - b.rank! || a.stableOrder - b.stableOrder)
.map((entry) => entry.item)
}

function getMatchRank(
item: Pick<NativeChatPickerItem, 'name' | 'description'>,
query: string
query: string,
matchDescriptions: boolean
): number | null {
const normalizedQuery = query.toLocaleLowerCase()
const name = item.name.toLocaleLowerCase()
Expand All @@ -230,7 +235,7 @@ function getMatchRank(
if (isSubsequence(normalizedQuery, name)) {
return 3
}
if (item.description?.toLocaleLowerCase().includes(normalizedQuery)) {
if (matchDescriptions && item.description?.toLocaleLowerCase().includes(normalizedQuery)) {
return 4
}
return null
Expand Down Expand Up @@ -301,26 +306,47 @@ function comparePickerSkills(
)
}

// `/` is the composer's only trigger, for every agent. A draft-leading slash is
// the one that can dispatch; elsewhere the token starts after whitespace and its
// query stops at the next `/` so file paths stay prose.
// `/` is the composer's trigger for every agent. A draft-leading slash is the one
// that can dispatch; elsewhere the token starts after whitespace and its query
// stops at the next `/` so file paths stay prose.
export const LEADING_SLASH_TRIGGER = /^\/(\S*)$/
export const MID_PROMPT_SLASH_TRIGGER = /\s\/([^\s/]*)$/
// An agent whose skills carry their own sigil (Codex `$`) also opens its skills alone on it.
const SKILL_SIGIL_TRIGGER = /(?:^|\s)\$(\S*)$/
const COMMON_ENV_VARS = new Set(
'PATH HOME USER SHELL PWD TMPDIR TEMP TMP LANG TERM XDG_CONFIG_HOME'.split(' ')
)

/** The query after a `$` skill sigil at the caret, or null when it reads as shell
* syntax (`$HOME`, `$5`, `$(pwd)`, `$?`), by the same rule as Codex's own composer. */
export function matchSkillSigil(before: string): string | null {
const query = before.match(SKILL_SIGIL_TRIGGER)?.[1] ?? null
// A bare `$` still opens the full list.
if (!query) {
return query
}
const name = query.match(/^[\w:-]*/)?.[0] ?? ''
if (name === '' || name === '_' || /^[-\d]/.test(name)) {
return null
}
// Why: a lowercase spelling stays a skill, so a skill named `home` still completes.
return !/[a-z]/.test(name) && COMMON_ENV_VARS.has(name.toUpperCase()) ? null : query
}

/** Replaces the typed `/token` with the item's own token, which for a skill is
* the agent-native form even though every agent is typed the same way. */
/** Replaces the typed trigger token with the item's own token, which for a skill
* is the agent-native form whichever trigger opened the menu. */
export function applyPickerSuggestion(
draft: string,
caret: number,
item: NativeChatPickerItem
): { draft: string; caret: number; insertedToken: string } {
const before = draft.slice(0, caret)
const after = draft.slice(caret)
const match = before.match(LEADING_SLASH_TRIGGER) ?? before.match(MID_PROMPT_SLASH_TRIGGER)
if (!match) {
const slashMatch = before.match(LEADING_SLASH_TRIGGER) ?? before.match(MID_PROMPT_SLASH_TRIGGER)
const query = slashMatch?.[1] ?? (item.kind === 'skill' ? matchSkillSigil(before) : null)
if (query === null) {
return { draft, caret, insertedToken: '' }
}
const query = match.at(-1) ?? ''
const tokenStart = before.length - query.length - 1
const nextBefore = `${before.slice(0, tokenStart)}${item.token} `
return { draft: nextBefore + after, caret: nextBefore.length, insertedToken: item.token }
Expand Down
Loading