char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

char/aliceai-t5-moe
1.6 KiB81 linesraw
1// An `@` starts a mention only when preceded by start-of-string or one of
2// these; identifier chars are not delimiters, so a mid-word `@` does not.
3const TOKEN_BOUNDARY_CHARS = new Set([
4	' ',
5	'\t',
6	'\n',
7	'\r',
8	'(',
9	')',
10	'[',
11	']',
12	',',
13	';',
14	':',
15	'"',
16	"'"
17]);
18
19/**
20 * Find the most-recent `@`-mention token whose extent includes `cursor`;
21 * the query covers the whole `@...` token regardless of caret position.
22 */
23export function findMentionToken(
24	value: string,
25	cursor: number
26): { start: number; end: number; query: string } | null {
27	if (cursor <= 0 || cursor > value.length) return null;
28
29	let atIndex = -1;
30
31	for (let i = cursor - 1; i >= 0; i--) {
32		const ch = value[i];
33
34		if (ch === '@') {
35			const prev = i > 0 ? value[i - 1] : '';
36
37			if (i === 0 || TOKEN_BOUNDARY_CHARS.has(prev)) {
38				atIndex = i;
39			}
40
41			break;
42		}
43
44		if (TOKEN_BOUNDARY_CHARS.has(ch)) break;
45	}
46
47	if (atIndex === -1) return null;
48
49	let end = atIndex + 1;
50
51	while (end < value.length && !TOKEN_BOUNDARY_CHARS.has(value[end])) {
52		end++;
53	}
54
55	return {
56		end,
57		query: value.slice(atIndex + 1, end),
58		start: atIndex
59	};
60}
61
62/**
63 * Stable signature of a mention token for use as a "dismissed" marker:
64 * while the picker is closed and this exact token is still intact, the
65 * picker does not silently re-open on in-token edits.
66 */
67export interface MentionDismissSnapshot {
68	start: number;
69	query: string;
70}
71
72export function takeMentionDismissSnapshot(
73	value: string,
74	cursor: number
75): MentionDismissSnapshot | null {
76	const token = findMentionToken(value, cursor);
77
78	if (!token) return null;
79
80	return { query: token.query, start: token.start };
81}