| 1 | /** How g1t's agent is mentioned. The same as `AGENT_HANDLE` in the contracts. */ |
| 2 | export const AGENT_MENTION = "@g1t"; |
| 3 | |
| 4 | const NAME = AGENT_MENTION.slice(1); |
| 5 | |
| 6 | /** |
| 7 | * The `@…` being typed just before the caret, if it could become `@g1t`: |
| 8 | * where it starts. Null otherwise, and once it is `@g1t` already. |
| 9 | */ |
| 10 | export function partialMention(text: string, caret: number): number | null { |
| 11 | const match = /(?:^|[\s(])@([\w./-]{0,9})$/.exec(text.slice(0, caret)); |
| 12 | if (!match) return null; |
| 13 | const typed = match[1].toLowerCase(); |
| 14 | if (typed === NAME || !NAME.startsWith(typed)) return null; |
| 15 | return caret - typed.length - 1; |
| 16 | } |
| 17 | |
| 18 | /** The most suggestions shown at once. */ |
| 19 | export const MAX_SUGGESTIONS = 5; |
| 20 | |
| 21 | /** What could complete the `@…` before the caret: where it starts, and the handles that fit, best first. */ |
| 22 | export type MentionMatch = { start: number; options: string[] }; |
| 23 | |
| 24 | /** |
| 25 | * The `@…` being typed just before the caret and what could complete it: |
| 26 | * `@g1t` and the `handles` given (such as `@acme/backend`) whose name, or |
| 27 | * a team's own part after the `/`, starts with what is typed. At most |
| 28 | * `MAX_SUGGESTIONS`; nothing once one is typed in full. Null when the |
| 29 | * caret is not in a mention or nothing fits. |
| 30 | */ |
| 31 | export function mentionSuggestions(text: string, caret: number, handles: readonly string[] = []): MentionMatch | null { |
| 32 | const match = /(?:^|[\s(])@([\w./-]{0,100})$/.exec(text.slice(0, caret)); |
| 33 | if (!match) return null; |
| 34 | const typed = match[1].toLowerCase(); |
| 35 | const start = caret - match[1].length - 1; |
| 36 | const names = [...new Set([AGENT_MENTION, ...handles].map((handle) => (handle.startsWith("@") ? handle : `@${handle}`)))]; |
| 37 | if (names.some((name) => name.slice(1).toLowerCase() === typed)) return null; |
| 38 | const whole: string[] = []; |
| 39 | const part: string[] = []; |
| 40 | for (const name of names) { |
| 41 | const bare = name.slice(1).toLowerCase(); |
| 42 | if (bare.startsWith(typed)) whole.push(name); |
| 43 | else if (typed && !typed.includes("/") && bare.split("/")[1]?.startsWith(typed)) part.push(name); |
| 44 | } |
| 45 | const options = [...whole, ...part].slice(0, MAX_SUGGESTIONS); |
| 46 | return options.length > 0 ? { start, options } : null; |
| 47 | } |