feat(composer): unify prompt suggestions with fuzzy matching and a mobile keybar

This commit is contained in:
Chris Scott committed 2026-10-07 18:04:08 -04:00
1 parent 704f81884b
commit 855b78297e
24 files changed
+1121 -399

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest'
import { matchText, rankByMatch } from './fuzzyMatch'
describe('matchText', () => {
it.each([
['review', 'review', 0, [[0, 6]]],
['rev', 'review', 1, [[0, 3]]],
['pr', 'plan-review', 2, [[0, 1], [5, 6]]],
['view', 'review', 3, [[2, 6]]],
['rvw', 'review', 4, [[0, 1], [2, 3], [5, 6]]],
])('matches %j against %j at tier %i', (query, name, tier, ranges) => {
expect(matchText(query, name)).toEqual({ tier, ranges })
})
it('falls back to the description without highlighting the name', () => {
expect(matchText('deploy', 'ship', 'Deploy a preview')).toEqual({ tier: 5, ranges: [] })
})
it('returns null when nothing matches', () => {
expect(matchText('xyz', 'review', 'Review changes')).toBeNull()
})
})
describe('rankByMatch', () => {
const items = ['fix-tests', 'format-code', 'fetch', 'test']
it('orders by tier, then recency, then name', () => {
const ranked = rankByMatch(items, 'ft', { getName: (item) => item, recent: ['format-code'] })
expect(ranked.map(({ item }) => item)).toEqual(['fix-tests', 'format-code', 'fetch'])
expect(rankByMatch(items, 'f', { getName: (item) => item, recent: ['format-code'] }).map(({ item }) => item))
.toEqual(['format-code', 'fetch', 'fix-tests'])
})
})
+99
View File
@@ -0,0 +1,99 @@
export type MatchRange = [start: number, end: number]
export interface TextMatch {
tier: number
ranges: MatchRange[]
}
export interface RankedMatch<T> {
item: T
tier: number
ranges: MatchRange[]
}
interface RankOptions<T> {
getName: (item: T) => string
getDescription?: (item: T) => string | undefined
recent?: string[]
}
const SEGMENT_SEPARATOR = /[-_./\s]/
const MIN_DESCRIPTION_QUERY_LENGTH = 3
/**
* Matches a query against a name, falling back to the description for queries of three or more characters.
* Lower tiers are better: exact, prefix, segment acronym, substring, subsequence, description.
*/
export function matchText(query: string, name: string, description?: string): TextMatch | null {
const needle = query.toLowerCase()
const haystack = name.toLowerCase()
if (!needle) return { tier: 0, ranges: [] }
if (haystack === needle) return { tier: 0, ranges: [[0, haystack.length]] }
if (haystack.startsWith(needle)) return { tier: 1, ranges: [[0, needle.length]] }
const acronym = matchPositions(needle, haystack, segmentStarts(haystack))
if (acronym) return { tier: 2, ranges: toRanges(acronym) }
const index = haystack.indexOf(needle)
if (index !== -1) return { tier: 3, ranges: [[index, index + needle.length]] }
const subsequence = matchPositions(needle, haystack, haystack.split('').map((_, position) => position))
if (subsequence) return { tier: 4, ranges: toRanges(subsequence) }
if (needle.length >= MIN_DESCRIPTION_QUERY_LENGTH && description?.toLowerCase().includes(needle)) return { tier: 5, ranges: [] }
return null
}
/**
* Filters and orders items by match tier, then by position in `recent`, then alphabetically.
* An empty query lists recent items first, followed by the rest alphabetically.
*/
export function rankByMatch<T>(items: T[], query: string, options: RankOptions<T>): RankedMatch<T>[] {
const recent = options.recent ?? []
const recentIndex = (item: T) => {
const index = recent.indexOf(options.getName(item))
return index === -1 ? recent.length : index
}
return items
.flatMap((item) => {
const match = matchText(query, options.getName(item), options.getDescription?.(item))
return match ? [{ item, match }] : []
})
.sort((a, b) =>
a.match.tier - b.match.tier
|| recentIndex(a.item) - recentIndex(b.item)
|| options.getName(a.item).localeCompare(options.getName(b.item)),
)
.map(({ item, match }) => ({ item, tier: match.tier, ranges: match.ranges }))
}
function segmentStarts(text: string): number[] {
return text.split('').flatMap((char, index) =>
!SEGMENT_SEPARATOR.test(char) && (index === 0 || SEGMENT_SEPARATOR.test(text[index - 1])) ? [index] : [],
)
}
function matchPositions(needle: string, haystack: string, candidates: number[]): number[] | null {
const positions: number[] = []
let cursor = 0
for (const char of needle) {
while (cursor < candidates.length && haystack[candidates[cursor]] !== char) cursor++
if (cursor === candidates.length) return null
positions.push(candidates[cursor])
cursor++
}
return positions
}
function toRanges(positions: number[]): MatchRange[] {
return positions.reduce<MatchRange[]>((ranges, position) => {
const last = ranges[ranges.length - 1]
if (last && last[1] === position) {
last[1] = position + 1
return ranges
}
ranges.push([position, position + 1])
return ranges
}, [])
}
+13 -11
View File
@@ -10,9 +10,9 @@ export interface MentionTrigger {
query: string
}
export interface AgentInfo {
name: string
description?: string
export interface MentionItem {
type: 'file' | 'agent' | 'skill'
value: string
}
export function detectMentionTrigger(
@@ -32,13 +32,6 @@ export function detectMentionTrigger(
}
}
export function filterAgentsByQuery(agents: AgentInfo[], query: string): AgentInfo[] {
const lowerQuery = query.toLowerCase()
return agents.filter(agent =>
agent.name.toLowerCase().includes(lowerQuery)
)
}
export interface ParsedPromptInput {
text: string
files: PromptFileInput[]
@@ -50,11 +43,14 @@ export function parsePromptToInput(
rawInput: string,
fileMap: Map<string, FileAttachmentInfo>,
agentNames: string[],
skillIds: string[],
imageAttachments?: ImageAttachment[]
): ParsedPromptInput {
const files: PromptFileInput[] = []
const agents: PromptAgentInput[] = []
const skills: PromptSkillInput[] = []
const agentNameByLowercase = new Map(agentNames.map((name) => [name.toLowerCase(), name]))
const skillIdByLowercase = new Map(skillIds.map((id) => [id.toLowerCase(), id]))
for (const match of rawInput.matchAll(MENTION_PATTERN)) {
const matchIndex = match.index!
@@ -70,6 +66,12 @@ export function parsePromptToInput(
const agentName = agentNameByLowercase.get(mentionText.toLowerCase())
if (agentName) {
agents.push({ name: agentName, mention })
continue
}
const skillId = skillIdByLowercase.get(mentionText.toLowerCase())
if (skillId && !skills.some((skill) => skill.id === skillId)) {
skills.push({ id: skillId, mention })
}
}
@@ -77,7 +79,7 @@ export function parsePromptToInput(
files.push({ uri: attachment.dataUrl, name: attachment.filename })
}
return { text: rawInput, files, agents, skills: [] }
return { text: rawInput, files, agents, skills }
}
export function getFilename(path: string): string {