g1t/apps/web/app/lib/mention.ts
| 1 | /** How g1t's agent is mentioned. The same as `AGENT_HANDLE` in the contracts. */ |
| 2 | export const AGENT_MENTION = "@g1t-agent"; |
| 3 | |
| 4 | const NAME = AGENT_MENTION.slice(1); |
| 5 | |
| 6 | /** |
| 7 | * The `@…` being typed just before the caret, if it could become |
| 8 | * `@g1t-agent`: where it starts. Null otherwise. |
| 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 | } |