| 1 | export type SlashCommand = { |
| 2 | name: string; |
| 3 | description: string; |
| 4 | }; |
| 5 | |
| 6 | export type SlashCommandMatch = SlashCommand & { |
| 7 | matchedPrefix: string; |
| 8 | unmatchedSuffix: string; |
| 9 | }; |
| 10 | |
| 11 | export const SLASH_COMMANDS: SlashCommand[] = [ |
| 12 | {name: '/compact', description: 'Compact the current session context'}, |
| 13 | ]; |
| 14 | |
| 15 | export function matchingSlashCommands(input: string): SlashCommandMatch[] { |
| 16 | if (!input.startsWith('/')) return []; |
| 17 | const query = slashCommandQuery(input); |
| 18 | return SLASH_COMMANDS |
| 19 | .filter((command) => command.name.toLowerCase().startsWith(query.toLowerCase())) |
| 20 | .map((command) => ({ |
| 21 | ...command, |
| 22 | matchedPrefix: command.name.slice(0, query.length), |
| 23 | unmatchedSuffix: command.name.slice(query.length), |
| 24 | })); |
| 25 | } |
| 26 | |
| 27 | export function slashCommandQuery(input: string): string { |
| 28 | return input.trimStart().split(/\s+/, 1)[0] ?? ''; |
| 29 | } |
| 30 | |
| 31 | export function shouldShowSlashCommands(input: string, busy: boolean): boolean { |
| 32 | return !busy && input.startsWith('/'); |
| 33 | } |
| 34 |