@kliebhan/pi-prompt-autocomplete 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core.ts ADDED
@@ -0,0 +1,1038 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readFileSync } from "node:fs";
3
+ import type { Api, Model } from "@earendil-works/pi-ai";
4
+
5
+ const TEMPLATE_VARIABLE_PATTERN = /(?<!\\)\{\{\s*([A-Z0-9_]+)\s*(?:\|\s*([\s\S]*?))?\s*\}\}/g;
6
+ const ESCAPED_TEMPLATE_VARIABLE_PATTERN = /\\(\{\{\s*[A-Z0-9_]+\s*(?:\|\s*[\s\S]*?)?\s*\}\})/g;
7
+ const PROMPT_AUTOCOMPLETE_RESPONSE_KEY = "completions";
8
+
9
+ export function normalizeTemplateText(template: string): string {
10
+ return template.replace(/\r\n/g, "\n").trim();
11
+ }
12
+
13
+ export function renderMiniTemplate(template: string, variables: Record<string, string>): string {
14
+ const missingVariables = new Set<string>();
15
+
16
+ const rendered = template.replace(
17
+ TEMPLATE_VARIABLE_PATTERN,
18
+ (_match, rawName: string, rawFallback: string | undefined) => {
19
+ const name = String(rawName);
20
+ const value = variables[name];
21
+ if (typeof value === "string") {
22
+ return value;
23
+ }
24
+
25
+ if (typeof rawFallback === "string") {
26
+ return rawFallback.trim();
27
+ }
28
+
29
+ missingVariables.add(name);
30
+ return `{{${name}}}`;
31
+ },
32
+ );
33
+
34
+ if (missingVariables.size > 0) {
35
+ throw new Error(`Missing template variable(s): ${[...missingVariables].sort().join(", ")}`);
36
+ }
37
+
38
+ return rendered.replace(ESCAPED_TEMPLATE_VARIABLE_PATTERN, "$1");
39
+ }
40
+
41
+ export const PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT_TEMPLATE_VARIABLES = {
42
+ RESPONSE_KEY: PROMPT_AUTOCOMPLETE_RESPONSE_KEY,
43
+ RESPONSE_EXAMPLE: JSON.stringify({ [PROMPT_AUTOCOMPLETE_RESPONSE_KEY]: ["suggestion 1", "suggestion 2"] }),
44
+ EMPTY_RESPONSE_EXAMPLE: JSON.stringify({ [PROMPT_AUTOCOMPLETE_RESPONSE_KEY]: [] }),
45
+ } as const;
46
+
47
+ export const PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT = renderMiniTemplate(
48
+ normalizeTemplateText(
49
+ readFileSync(
50
+ new URL("./system-prompt.template.md", import.meta.url),
51
+ "utf8",
52
+ ),
53
+ ),
54
+ PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT_TEMPLATE_VARIABLES,
55
+ );
56
+
57
+ export const DEFAULT_PREFERRED_MODEL = "current active model";
58
+ export const DEFAULT_PROMPT_AUTOCOMPLETE_ENABLED = false;
59
+ export const DEFAULT_DEBOUNCE_MS = 350;
60
+ export const DEFAULT_MIN_PROMPT_CHARS = 1;
61
+ export const DEFAULT_MAX_SUGGESTION_CHARS = 160;
62
+ export const DEFAULT_MAX_ALTERNATIVES = 3;
63
+ export const MAX_DRAFT_CONTEXT_CHARS = 2_000;
64
+ export const MAX_CONTEXT_MESSAGES = 6;
65
+ export const MAX_CONTEXT_MESSAGE_CHARS = 600;
66
+ export const MAX_LATEST_ASSISTANT_MESSAGE_CHARS = 3_000;
67
+ export const MAX_LATEST_USER_MESSAGE_CHARS = 1_200;
68
+ // Scale the completion budget with the requested alternatives so a JSON array of
69
+ // several short suggestions cannot be truncated mid-string (which would break parsing).
70
+ export const MIN_REQUEST_MAX_TOKENS = 192;
71
+ export const MAX_REQUEST_MAX_TOKENS = 1_024;
72
+
73
+ export interface ModelRef {
74
+ provider: string;
75
+ id: string;
76
+ }
77
+
78
+ export interface PromptAutocompleteCacheIdentity {
79
+ leafId: string;
80
+ modelLabel: string;
81
+ maxAlternatives: number;
82
+ maxSuggestionChars: number;
83
+ draft: string;
84
+ latestAssistantContext: string;
85
+ latestUserContext: string;
86
+ recentContext: string;
87
+ }
88
+
89
+ export function buildPromptAutocompleteCacheKey(identity: PromptAutocompleteCacheIdentity): string {
90
+ const digest = createHash("sha256")
91
+ .update(JSON.stringify(identity))
92
+ .digest("hex");
93
+ return `${identity.modelLabel}|${digest}`;
94
+ }
95
+
96
+ export class ExpiringLruCache<T> {
97
+ private readonly entries = new Map<string, { value: T; expiresAt: number }>();
98
+ private readonly ttlMs: number;
99
+ private readonly maxEntries: number;
100
+ private readonly now: () => number;
101
+
102
+ constructor(ttlMs: number, maxEntries: number, now: () => number = Date.now) {
103
+ this.ttlMs = ttlMs;
104
+ this.maxEntries = maxEntries;
105
+ this.now = now;
106
+ }
107
+
108
+ get size(): number {
109
+ this.prune();
110
+ return this.entries.size;
111
+ }
112
+
113
+ get(key: string, options: { bypass?: boolean } = {}): T | undefined {
114
+ if (options.bypass) return undefined;
115
+
116
+ const entry = this.entries.get(key);
117
+ if (!entry) return undefined;
118
+ if (entry.expiresAt <= this.now()) {
119
+ this.entries.delete(key);
120
+ return undefined;
121
+ }
122
+
123
+ this.entries.delete(key);
124
+ this.entries.set(key, entry);
125
+ return entry.value;
126
+ }
127
+
128
+ set(key: string, value: T): void {
129
+ this.entries.delete(key);
130
+ this.entries.set(key, { value, expiresAt: this.now() + this.ttlMs });
131
+ this.prune();
132
+ }
133
+
134
+ clear(): void {
135
+ this.entries.clear();
136
+ }
137
+
138
+ private prune(): void {
139
+ const now = this.now();
140
+ for (const [key, entry] of this.entries) {
141
+ if (entry.expiresAt <= now) this.entries.delete(key);
142
+ }
143
+
144
+ while (this.entries.size > this.maxEntries) {
145
+ const oldestKey = this.entries.keys().next().value as string | undefined;
146
+ if (oldestKey === undefined) break;
147
+ this.entries.delete(oldestKey);
148
+ }
149
+ }
150
+ }
151
+
152
+ export class SequenceOwnedSlot<T extends { seq: number }> {
153
+ private value?: T;
154
+
155
+ get current(): T | undefined {
156
+ return this.value;
157
+ }
158
+
159
+ set(value: T): void {
160
+ this.value = value;
161
+ }
162
+
163
+ clearIfOwned(seq: number): boolean {
164
+ if (this.value?.seq !== seq) return false;
165
+ this.value = undefined;
166
+ return true;
167
+ }
168
+
169
+ take(): T | undefined {
170
+ const value = this.value;
171
+ this.value = undefined;
172
+ return value;
173
+ }
174
+ }
175
+
176
+ export interface CoalescedRequestEntry<T> {
177
+ promise: Promise<T>;
178
+ controller: AbortController;
179
+ subscribers: Set<string>;
180
+ settled: boolean;
181
+ }
182
+
183
+ export interface CoalescedRequestSubscription<T> {
184
+ promise: Promise<T>;
185
+ created: boolean;
186
+ release: () => void;
187
+ subscriberCount: () => number;
188
+ }
189
+
190
+ export interface OwnerRefCounter {
191
+ activate: (owner: string) => boolean;
192
+ deactivate: (owner: string) => boolean;
193
+ clear: () => boolean;
194
+ has: (owner: string) => boolean;
195
+ size: () => number;
196
+ }
197
+
198
+ export function createOwnerRefCounter(): OwnerRefCounter {
199
+ const owners = new Set<string>();
200
+
201
+ return {
202
+ activate: (owner) => {
203
+ const wasEmpty = owners.size === 0;
204
+ owners.add(owner);
205
+ return wasEmpty && owners.size > 0;
206
+ },
207
+ deactivate: (owner) => {
208
+ const deleted = owners.delete(owner);
209
+ return deleted && owners.size === 0;
210
+ },
211
+ clear: () => {
212
+ const hadOwners = owners.size > 0;
213
+ owners.clear();
214
+ return hadOwners;
215
+ },
216
+ has: (owner) => owners.has(owner),
217
+ size: () => owners.size,
218
+ };
219
+ }
220
+
221
+ export function acquireCoalescedRequest<T>(
222
+ inFlightRequests: Map<string, CoalescedRequestEntry<T>>,
223
+ key: string,
224
+ subscriberId: string,
225
+ start: (signal: AbortSignal) => Promise<T>,
226
+ ): CoalescedRequestSubscription<T> {
227
+ let entry = inFlightRequests.get(key);
228
+ let created = false;
229
+
230
+ if (!entry) {
231
+ const controller = new AbortController();
232
+ const subscribers = new Set<string>();
233
+ let runStart: () => void = () => undefined;
234
+ const promise = new Promise<T>((resolve, reject) => {
235
+ runStart = () => {
236
+ try {
237
+ Promise.resolve(start(controller.signal)).then(resolve, reject);
238
+ } catch (error) {
239
+ reject(error);
240
+ }
241
+ };
242
+ });
243
+ let entryForFinally: CoalescedRequestEntry<T> | undefined;
244
+ const settledPromise = promise.finally(() => {
245
+ if (!entryForFinally) return;
246
+ entryForFinally.settled = true;
247
+ if (inFlightRequests.get(key) === entryForFinally) {
248
+ inFlightRequests.delete(key);
249
+ }
250
+ });
251
+ const nextEntry: CoalescedRequestEntry<T> = {
252
+ controller,
253
+ subscribers,
254
+ settled: false,
255
+ promise: settledPromise,
256
+ };
257
+ entryForFinally = nextEntry;
258
+
259
+ entry = nextEntry;
260
+ inFlightRequests.set(key, entry);
261
+ created = true;
262
+ // Start only after the entry is visible in the map, so synchronous start
263
+ // failures still settle through the shared promise and clean up the map.
264
+ runStart();
265
+ }
266
+
267
+ entry.subscribers.add(subscriberId);
268
+ let released = false;
269
+
270
+ return {
271
+ promise: entry.promise,
272
+ created,
273
+ subscriberCount: () => entry.subscribers.size,
274
+ release: () => {
275
+ // Idempotent because callers may release during cancellation and again in
276
+ // their async finally block after the shared promise settles.
277
+ if (released) return;
278
+ released = true;
279
+
280
+ const current = inFlightRequests.get(key);
281
+ if (current !== entry) return;
282
+
283
+ current.subscribers.delete(subscriberId);
284
+ if (current.subscribers.size === 0 && !current.settled) {
285
+ current.controller.abort();
286
+ inFlightRequests.delete(key);
287
+ }
288
+ },
289
+ };
290
+ }
291
+
292
+ export function cancelAllCoalescedRequests<T>(inFlightRequests: Map<string, CoalescedRequestEntry<T>>): void {
293
+ for (const entry of inFlightRequests.values()) {
294
+ if (!entry.settled) {
295
+ entry.controller.abort();
296
+ }
297
+ }
298
+ inFlightRequests.clear();
299
+ }
300
+
301
+ interface SuggestionPayload {
302
+ completions?: unknown;
303
+ suggestions?: unknown;
304
+ alternatives?: unknown;
305
+ items?: unknown;
306
+ completion?: unknown;
307
+ suggestion?: unknown;
308
+ text?: unknown;
309
+ }
310
+
311
+ function isRecord(value: unknown): value is Record<string, unknown> {
312
+ return typeof value === "object" && value !== null && !Array.isArray(value);
313
+ }
314
+
315
+ function trimAndCollapse(text: string): string {
316
+ return text.trim().replace(/\s+/g, " ");
317
+ }
318
+
319
+ function truncateWithEllipsis(text: string, maxChars: number): string {
320
+ if (text.length <= maxChars) return text;
321
+ if (maxChars <= 1) return "…";
322
+ return `${text.slice(0, maxChars - 1)}…`;
323
+ }
324
+
325
+ function truncateWithForcedEllipsis(text: string, maxChars: number): string {
326
+ if (maxChars <= 0) return "";
327
+ if (maxChars === 1) return "…";
328
+ return `${text.slice(0, maxChars - 1)}…`;
329
+ }
330
+
331
+ function truncateMiddle(text: string, maxChars: number): string {
332
+ if (text.length <= maxChars) return text;
333
+ if (maxChars <= 5) return truncateWithEllipsis(text, maxChars);
334
+
335
+ const separator = "\n…\n";
336
+ const remaining = maxChars - separator.length;
337
+ const head = Math.ceil(remaining / 2);
338
+ const tail = Math.floor(remaining / 2);
339
+ return `${text.slice(0, head)}${separator}${text.slice(-tail)}`;
340
+ }
341
+
342
+ function stripCodeFences(text: string): string {
343
+ const trimmed = text.trim();
344
+ if (!trimmed.startsWith("```")) {
345
+ return text;
346
+ }
347
+
348
+ const withoutOpening = trimmed.replace(/^```[^\n]*\n?/, "");
349
+ return withoutOpening.replace(/\n?```\s*$/, "");
350
+ }
351
+
352
+ function stripWrappingQuotes(text: string): string {
353
+ const trimmed = text.trim();
354
+ if (trimmed.length < 2) return text;
355
+
356
+ const pairs: Array<[string, string]> = [
357
+ ['"', '"'],
358
+ ["'", "'"],
359
+ ["`", "`"],
360
+ ];
361
+
362
+ for (const [start, end] of pairs) {
363
+ if (trimmed.startsWith(start) && trimmed.endsWith(end)) {
364
+ return trimmed.slice(1, -1);
365
+ }
366
+ }
367
+
368
+ return text;
369
+ }
370
+
371
+ function stripRepeatedDraftPrefix(draft: string, suggestion: string): string {
372
+ if (!draft) return suggestion;
373
+
374
+ if (suggestion.startsWith(draft)) {
375
+ return suggestion.slice(draft.length);
376
+ }
377
+
378
+ const horizontallyTrimmedSuggestion = suggestion.replace(/^[ \t]+/, "");
379
+ if (horizontallyTrimmedSuggestion.startsWith(draft)) {
380
+ return horizontallyTrimmedSuggestion.slice(draft.length);
381
+ }
382
+
383
+ return suggestion;
384
+ }
385
+
386
+ function getTrailingWordFragment(text: string): string {
387
+ return /([\p{L}\p{M}\p{N}_]+)$/u.exec(text)?.[1] ?? "";
388
+ }
389
+
390
+ function startsWithCaseInsensitivePrefix(text: string, prefix: string): boolean {
391
+ if (text.length < prefix.length) return false;
392
+ return text.slice(0, prefix.length).toLocaleLowerCase() === prefix.toLocaleLowerCase();
393
+ }
394
+
395
+ function stripRepeatedCurrentWordPrefix(draft: string, suggestion: string): string {
396
+ const currentWord = getTrailingWordFragment(draft);
397
+ if (!currentWord) return suggestion;
398
+
399
+ // If the model returns the expanded current word instead of only the suffix
400
+ // (e.g. draft "Schrei" -> suggestion "Schreibe ..."), keep only the text
401
+ // that is still missing at the cursor. This prevents accepting the suggestion
402
+ // from duplicating or separating the partially typed word.
403
+ const horizontallyTrimmedSuggestion = suggestion.replace(/^[ \t]+/, "");
404
+ if (startsWithCaseInsensitivePrefix(horizontallyTrimmedSuggestion, currentWord)) {
405
+ return horizontallyTrimmedSuggestion.slice(currentWord.length);
406
+ }
407
+
408
+ return suggestion;
409
+ }
410
+
411
+ function normalizeLeadingBoundarySpacing(draft: string, suggestion: string): string {
412
+ if (!suggestion) return suggestion;
413
+
414
+ const lastChar = draft.slice(-1);
415
+ if (!lastChar) return suggestion.replace(/^[ \t]+/, "");
416
+
417
+ // If the draft already ends with a horizontal whitespace, do not keep another
418
+ // leading horizontal whitespace from the suggestion.
419
+ if (/[ \t]/.test(lastChar)) {
420
+ return suggestion.replace(/^[ \t]+/, "");
421
+ }
422
+
423
+ // If the model starts with multiple spaces/tabs even though the draft does not
424
+ // end with whitespace, collapse them to a single separating space.
425
+ if (/^[ \t]+/.test(suggestion)) {
426
+ return ` ${suggestion.trimStart()}`;
427
+ }
428
+
429
+ return suggestion;
430
+ }
431
+
432
+ function maybePrefixSpace(draft: string, suggestion: string): string {
433
+ if (!suggestion) return suggestion;
434
+ if (/^\s/.test(suggestion)) return suggestion;
435
+
436
+ const lastChar = draft.slice(-1);
437
+ if (!lastChar) return suggestion;
438
+
439
+ if (/\s/.test(lastChar)) return suggestion;
440
+ if (/[({\["'`/\\-]/.test(lastChar)) return suggestion;
441
+ if (/^[,.;:!?)}\]]/.test(suggestion)) return suggestion;
442
+
443
+ // Do not infer a missing word boundary between two word characters. The
444
+ // cursor may be inside a partially typed word ("Schrei" + "be ..."), and
445
+ // adding a space would corrupt the accepted completion. For word-to-word
446
+ // continuations we rely on the model-provided leading whitespace instead.
447
+ if (/[A-Za-z0-9]/.test(lastChar) && /^[A-Za-z0-9]/.test(suggestion)) {
448
+ return suggestion;
449
+ }
450
+
451
+ if (/[A-Za-z0-9\])]/.test(lastChar) && /^[([{"']/.test(suggestion)) {
452
+ return ` ${suggestion}`;
453
+ }
454
+
455
+ if (/[\])]/.test(lastChar) && /^[A-Za-z0-9([{"']/.test(suggestion)) {
456
+ return ` ${suggestion}`;
457
+ }
458
+
459
+ if (/[,:;]/.test(lastChar) && !/^\s/.test(suggestion)) {
460
+ return ` ${suggestion}`;
461
+ }
462
+
463
+ return suggestion;
464
+ }
465
+
466
+ function getJsonCandidates(text: string): string[] {
467
+ const stripped = stripCodeFences(text).trim();
468
+ const candidates = new Set<string>();
469
+
470
+ if (stripped) {
471
+ candidates.add(stripped);
472
+ }
473
+
474
+ const objectStart = stripped.indexOf("{");
475
+ const objectEnd = stripped.lastIndexOf("}");
476
+ if (objectStart !== -1 && objectEnd !== -1 && objectEnd > objectStart) {
477
+ candidates.add(stripped.slice(objectStart, objectEnd + 1));
478
+ }
479
+
480
+ const arrayStart = stripped.indexOf("[");
481
+ const arrayEnd = stripped.lastIndexOf("]");
482
+ // Only salvage a bracketed substring as a standalone JSON array when it is not
483
+ // nested inside an object-shaped payload. Otherwise an incomplete object such
484
+ // as {"items":["metadata"],"completions":["good","trunc can parse the
485
+ // completed inner items array before the structured truncation salvage runs.
486
+ if (
487
+ arrayStart !== -1
488
+ && arrayEnd !== -1
489
+ && arrayEnd > arrayStart
490
+ && (objectStart === -1 || arrayStart < objectStart)
491
+ ) {
492
+ candidates.add(stripped.slice(arrayStart, arrayEnd + 1));
493
+ }
494
+
495
+ return [...candidates];
496
+ }
497
+
498
+ const SUGGESTION_ARRAY_KEYS = ["completions", "suggestions", "alternatives", "items"] as const;
499
+ const SUGGESTION_ARRAY_KEY_PATTERN = /"(?:completions|suggestions|alternatives|items)"\s*:\s*\[/;
500
+ const JSON_CODE_FENCE_PATTERN = /```\s*(?:json)?[\s\S]*?(?:\{|\[)/i;
501
+
502
+ type SuggestionArrayKey = typeof SUGGESTION_ARRAY_KEYS[number];
503
+
504
+ interface JsonStringLiteral {
505
+ value: string;
506
+ end: number;
507
+ }
508
+
509
+ interface JsonSkipResult {
510
+ end: number;
511
+ complete: boolean;
512
+ }
513
+
514
+ function hasJsonPayloadIndicator(text: string): boolean {
515
+ const stripped = stripCodeFences(text).trim();
516
+ return (
517
+ stripped.startsWith("{")
518
+ || stripped.startsWith("[")
519
+ || SUGGESTION_ARRAY_KEY_PATTERN.test(text)
520
+ || JSON_CODE_FENCE_PATTERN.test(text)
521
+ );
522
+ }
523
+
524
+ function skipJsonWhitespace(text: string, index: number): number {
525
+ let i = index;
526
+ while (i < text.length && /\s/.test(text[i])) i += 1;
527
+ return i;
528
+ }
529
+
530
+ function readJsonStringLiteral(text: string, startIndex: number): JsonStringLiteral | undefined {
531
+ if (text[startIndex] !== '"') return undefined;
532
+
533
+ let i = startIndex + 1;
534
+ while (i < text.length) {
535
+ const char = text[i];
536
+ if (char === "\\") {
537
+ i += 2;
538
+ continue;
539
+ }
540
+ if (char === '"') {
541
+ try {
542
+ const parsed = JSON.parse(text.slice(startIndex, i + 1)) as unknown;
543
+ return typeof parsed === "string" ? { value: parsed, end: i + 1 } : undefined;
544
+ } catch {
545
+ return undefined;
546
+ }
547
+ }
548
+ i += 1;
549
+ }
550
+
551
+ return undefined;
552
+ }
553
+
554
+ function skipJsonStringLiteral(text: string, startIndex: number): JsonSkipResult {
555
+ if (text[startIndex] !== '"') return { end: startIndex, complete: false };
556
+
557
+ let i = startIndex + 1;
558
+ while (i < text.length) {
559
+ const char = text[i];
560
+ if (char === "\\") {
561
+ i += 2;
562
+ continue;
563
+ }
564
+ if (char === '"') {
565
+ return { end: i + 1, complete: true };
566
+ }
567
+ i += 1;
568
+ }
569
+
570
+ return { end: text.length, complete: false };
571
+ }
572
+
573
+ function skipJsonValue(text: string, startIndex: number): JsonSkipResult {
574
+ let i = skipJsonWhitespace(text, startIndex);
575
+ if (i >= text.length) return { end: i, complete: false };
576
+
577
+ if (text[i] === '"') {
578
+ return skipJsonStringLiteral(text, i);
579
+ }
580
+
581
+ if (text[i] === "{" || text[i] === "[") {
582
+ const stack = [text[i] === "{" ? "}" : "]"];
583
+ i += 1;
584
+
585
+ while (i < text.length) {
586
+ const char = text[i];
587
+ if (char === '"') {
588
+ const skipped = skipJsonStringLiteral(text, i);
589
+ if (!skipped.complete) return skipped;
590
+ i = skipped.end;
591
+ continue;
592
+ }
593
+ if (char === "{") {
594
+ stack.push("}");
595
+ i += 1;
596
+ continue;
597
+ }
598
+ if (char === "[") {
599
+ stack.push("]");
600
+ i += 1;
601
+ continue;
602
+ }
603
+ if (char === stack[stack.length - 1]) {
604
+ stack.pop();
605
+ i += 1;
606
+ if (stack.length === 0) return { end: i, complete: true };
607
+ continue;
608
+ }
609
+ i += 1;
610
+ }
611
+
612
+ return { end: text.length, complete: false };
613
+ }
614
+
615
+ while (i < text.length && !/[,}\]]/.test(text[i])) i += 1;
616
+ return { end: i, complete: i > startIndex };
617
+ }
618
+
619
+ function scanTopLevelJsonStringArrayElements(text: string, startIndex: number): string[] {
620
+ const results: string[] = [];
621
+ let i = startIndex;
622
+
623
+ while (i < text.length) {
624
+ i = skipJsonWhitespace(text, i);
625
+ const char = text[i];
626
+ if (char === "]") break;
627
+ if (char === ",") {
628
+ i += 1;
629
+ continue;
630
+ }
631
+
632
+ if (char === '"') {
633
+ const literal = readJsonStringLiteral(text, i);
634
+ // Truncated or invalid top-level string: discard it instead of leaking it.
635
+ if (!literal) break;
636
+ results.push(literal.value);
637
+ i = literal.end;
638
+ } else {
639
+ const skipped = skipJsonValue(text, i);
640
+ if (!skipped.complete || skipped.end <= i) break;
641
+ i = skipped.end;
642
+ }
643
+
644
+ i = skipJsonWhitespace(text, i);
645
+ if (text[i] === ",") i += 1;
646
+ }
647
+
648
+ return results;
649
+ }
650
+
651
+ function extractPrioritizedSalvagedSuggestions(
652
+ text: string,
653
+ starts: Partial<Record<SuggestionArrayKey, number>>,
654
+ ): string[] | undefined {
655
+ let sawSuggestionArray = false;
656
+
657
+ for (const key of SUGGESTION_ARRAY_KEYS) {
658
+ const start = starts[key];
659
+ if (typeof start !== "number") continue;
660
+ sawSuggestionArray = true;
661
+
662
+ const suggestions = scanTopLevelJsonStringArrayElements(text, start);
663
+ if (suggestions.length > 0) return suggestions;
664
+ }
665
+
666
+ return sawSuggestionArray ? [] : undefined;
667
+ }
668
+
669
+ function findTopLevelSuggestionArrayStartsInObject(
670
+ text: string,
671
+ objectStartIndex: number,
672
+ ): Partial<Record<SuggestionArrayKey, number>> {
673
+ const starts: Partial<Record<SuggestionArrayKey, number>> = {};
674
+ let i = objectStartIndex + 1;
675
+
676
+ while (i < text.length) {
677
+ i = skipJsonWhitespace(text, i);
678
+ if (text[i] === ",") {
679
+ i += 1;
680
+ continue;
681
+ }
682
+ if (text[i] === "}") break;
683
+ if (text[i] !== '"') {
684
+ const skipped = skipJsonValue(text, i);
685
+ if (!skipped.complete || skipped.end <= i) break;
686
+ i = skipped.end;
687
+ continue;
688
+ }
689
+
690
+ const keyLiteral = readJsonStringLiteral(text, i);
691
+ if (!keyLiteral) break;
692
+ i = skipJsonWhitespace(text, keyLiteral.end);
693
+ if (text[i] !== ":") {
694
+ i = keyLiteral.end;
695
+ continue;
696
+ }
697
+
698
+ i = skipJsonWhitespace(text, i + 1);
699
+ const key = keyLiteral.value as SuggestionArrayKey;
700
+ if (
701
+ text[i] === "["
702
+ && (SUGGESTION_ARRAY_KEYS as readonly string[]).includes(keyLiteral.value)
703
+ && starts[key] === undefined
704
+ ) {
705
+ starts[key] = i + 1;
706
+ }
707
+
708
+ const skipped = skipJsonValue(text, i);
709
+ if (!skipped.complete || skipped.end <= i) break;
710
+ i = skipped.end;
711
+ }
712
+
713
+ return starts;
714
+ }
715
+
716
+ function salvageTruncatedJsonSuggestions(text: string): string[] {
717
+ const stripped = stripCodeFences(text).trim();
718
+
719
+ if (stripped.startsWith("[")) {
720
+ return scanTopLevelJsonStringArrayElements(stripped, 1);
721
+ }
722
+
723
+ let i = 0;
724
+ while (i < stripped.length) {
725
+ if (stripped[i] !== "{") {
726
+ i += 1;
727
+ continue;
728
+ }
729
+
730
+ const starts = findTopLevelSuggestionArrayStartsInObject(stripped, i);
731
+ const suggestions = extractPrioritizedSalvagedSuggestions(stripped, starts);
732
+ if (suggestions !== undefined) return suggestions;
733
+
734
+ const skipped = skipJsonValue(stripped, i);
735
+ i = skipped.complete && skipped.end > i ? skipped.end : i + 1;
736
+ }
737
+
738
+ return [];
739
+ }
740
+
741
+ function toStringArray(value: unknown): string[] {
742
+ if (!Array.isArray(value)) return [];
743
+ return value.filter((item): item is string => typeof item === "string");
744
+ }
745
+
746
+ function extractJsonSuggestions(value: unknown): string[] {
747
+ if (Array.isArray(value)) {
748
+ return toStringArray(value);
749
+ }
750
+
751
+ if (!isRecord(value)) {
752
+ return [];
753
+ }
754
+
755
+ const payload = value as SuggestionPayload;
756
+ const arrayKeys = [payload.completions, payload.suggestions, payload.alternatives, payload.items];
757
+ for (const candidate of arrayKeys) {
758
+ const strings = toStringArray(candidate);
759
+ if (strings.length > 0) return strings;
760
+ }
761
+
762
+ const scalarKeys = [payload.completion, payload.suggestion, payload.text];
763
+ for (const candidate of scalarKeys) {
764
+ if (typeof candidate === "string") {
765
+ return [candidate];
766
+ }
767
+ }
768
+
769
+ return [];
770
+ }
771
+
772
+ function parseSuggestionResponse(rawResponse: string): string[] {
773
+ for (const candidate of getJsonCandidates(rawResponse)) {
774
+ try {
775
+ const parsed = JSON.parse(candidate) as unknown;
776
+ const suggestions = extractJsonSuggestions(parsed);
777
+ if (suggestions.length > 0) {
778
+ return suggestions;
779
+ }
780
+
781
+ if (Array.isArray(parsed) && parsed.length === 0) {
782
+ return [];
783
+ }
784
+
785
+ if (isRecord(parsed) && Array.isArray((parsed as SuggestionPayload).completions)) {
786
+ return [];
787
+ }
788
+ } catch {
789
+ continue;
790
+ }
791
+ }
792
+
793
+ // The response looks like a structured JSON payload but could not be parsed,
794
+ // typically because the completion was truncated mid-string (stopReason
795
+ // "length"). Salvage the complete entries instead of leaking raw JSON into
796
+ // the editor as ghost text.
797
+ if (hasJsonPayloadIndicator(rawResponse)) {
798
+ return salvageTruncatedJsonSuggestions(rawResponse);
799
+ }
800
+
801
+ return rawResponse.trim() ? [rawResponse] : [];
802
+ }
803
+
804
+ export function parseModelRef(value: boolean | string | undefined): ModelRef | undefined {
805
+ if (typeof value !== "string") return undefined;
806
+ const trimmed = value.trim();
807
+ if (!trimmed) return undefined;
808
+
809
+ const slashIndex = trimmed.indexOf("/");
810
+ if (slashIndex <= 0 || slashIndex === trimmed.length - 1) {
811
+ return undefined;
812
+ }
813
+
814
+ return {
815
+ provider: trimmed.slice(0, slashIndex),
816
+ id: trimmed.slice(slashIndex + 1),
817
+ };
818
+ }
819
+
820
+ export function parseBoundedIntFlag(
821
+ value: boolean | string | undefined,
822
+ fallback: number,
823
+ min: number,
824
+ max: number,
825
+ ): number {
826
+ if (typeof value !== "string") return fallback;
827
+ const parsed = Number.parseInt(value.trim(), 10);
828
+ if (!Number.isFinite(parsed)) return fallback;
829
+ return Math.max(min, Math.min(max, parsed));
830
+ }
831
+
832
+ export function computeRequestMaxTokens(maxAlternatives: number, maxSuggestionChars: number): number {
833
+ // Rough chars→tokens estimate per suggestion plus JSON quoting/comma overhead,
834
+ // and a fixed wrapper budget for the surrounding {"completions":[...]} envelope.
835
+ const tokensPerSuggestion = Math.ceil(maxSuggestionChars / 3) + 8;
836
+ const estimated = 24 + maxAlternatives * tokensPerSuggestion;
837
+ return Math.max(MIN_REQUEST_MAX_TOKENS, Math.min(MAX_REQUEST_MAX_TOKENS, estimated));
838
+ }
839
+
840
+ export function extractMessageText(content: unknown, maxChars = Number.POSITIVE_INFINITY): string {
841
+ if (typeof content === "string") {
842
+ const trimmed = content.trim();
843
+ return Number.isFinite(maxChars) ? truncateMiddle(trimmed, maxChars) : trimmed;
844
+ }
845
+
846
+ if (!Array.isArray(content)) {
847
+ return "";
848
+ }
849
+
850
+ const parts: string[] = [];
851
+ let remaining = maxChars;
852
+ let truncated = false;
853
+
854
+ for (const block of content) {
855
+ if (!isRecord(block) || block.type !== "text" || typeof block.text !== "string") continue;
856
+ const text = block.text;
857
+
858
+ if (Number.isFinite(maxChars)) {
859
+ const separatorLength = parts.length > 0 ? 1 : 0;
860
+ if (remaining <= separatorLength) {
861
+ truncated = true;
862
+ break;
863
+ }
864
+
865
+ const availableForText = Math.max(0, remaining - separatorLength);
866
+ if (text.length > availableForText) {
867
+ parts.push(text.slice(0, availableForText));
868
+ truncated = true;
869
+ break;
870
+ }
871
+
872
+ remaining -= separatorLength + text.length;
873
+ }
874
+
875
+ parts.push(text);
876
+ }
877
+
878
+ const joined = parts.join("\n").trim();
879
+ if (!Number.isFinite(maxChars)) return joined;
880
+ return truncated ? truncateWithForcedEllipsis(joined, maxChars) : joined;
881
+ }
882
+
883
+ export function buildRecentConversationContext(
884
+ branch: unknown[],
885
+ maxMessages = MAX_CONTEXT_MESSAGES,
886
+ maxCharsPerMessage = MAX_CONTEXT_MESSAGE_CHARS,
887
+ ): string {
888
+ const collected: string[] = [];
889
+
890
+ for (let i = branch.length - 1; i >= 0 && collected.length < maxMessages; i -= 1) {
891
+ const entry = branch[i];
892
+ if (!isRecord(entry) || entry.type !== "message") continue;
893
+
894
+ const message = entry.message;
895
+ if (!isRecord(message)) continue;
896
+
897
+ const role = message.role;
898
+ if (role !== "user" && role !== "assistant") continue;
899
+
900
+ const text = extractMessageText(message.content, maxCharsPerMessage * 4);
901
+ if (!text) continue;
902
+
903
+ const prefix = role === "user" ? "User" : "Assistant";
904
+ const normalized = truncateWithEllipsis(trimAndCollapse(text), maxCharsPerMessage);
905
+ collected.push(`${prefix}: ${normalized}`);
906
+ }
907
+
908
+ return collected.reverse().join("\n\n");
909
+ }
910
+
911
+ function buildLatestRoleMessageContext(
912
+ branch: unknown[],
913
+ role: "user" | "assistant",
914
+ maxChars: number,
915
+ ): string {
916
+ for (let i = branch.length - 1; i >= 0; i -= 1) {
917
+ const entry = branch[i];
918
+ if (!isRecord(entry) || entry.type !== "message") continue;
919
+
920
+ const message = entry.message;
921
+ if (!isRecord(message) || message.role !== role) continue;
922
+
923
+ const text = extractMessageText(message.content, maxChars * 2);
924
+ if (!text) continue;
925
+
926
+ return truncateMiddle(text.trim(), maxChars);
927
+ }
928
+
929
+ return "";
930
+ }
931
+
932
+ export function buildLatestAssistantMessageContext(
933
+ branch: unknown[],
934
+ maxChars = MAX_LATEST_ASSISTANT_MESSAGE_CHARS,
935
+ ): string {
936
+ return buildLatestRoleMessageContext(branch, "assistant", maxChars);
937
+ }
938
+
939
+ export function buildLatestUserMessageContext(
940
+ branch: unknown[],
941
+ maxChars = MAX_LATEST_USER_MESSAGE_CHARS,
942
+ ): string {
943
+ return buildLatestRoleMessageContext(branch, "user", maxChars);
944
+ }
945
+
946
+ export function truncateDraftTail(draft: string, maxChars = MAX_DRAFT_CONTEXT_CHARS): string {
947
+ if (draft.length <= maxChars) return draft;
948
+ return draft.slice(-maxChars);
949
+ }
950
+
951
+ export function shouldSkipPromptAutocomplete(text: string): boolean {
952
+ const trimmedStart = text.trimStart();
953
+ if (trimmedStart.startsWith("/") || trimmedStart.startsWith("!")) return true;
954
+
955
+ const lastNewline = text.lastIndexOf("\n");
956
+ const textBeforeCursor = lastNewline === -1 ? text : text.slice(lastNewline + 1);
957
+
958
+ if (/(?:^|[ \t])[@#](?:"[^"]*|[^\s]*)$/.test(textBeforeCursor)) return true;
959
+ if (/(?:^|[ \t])(?:~\/|\.\.?\/|\/)[^\s]*$/.test(textBeforeCursor)) return true;
960
+
961
+ return false;
962
+ }
963
+
964
+ export function normalizePromptSuggestion(
965
+ draft: string,
966
+ rawSuggestion: string,
967
+ maxChars = DEFAULT_MAX_SUGGESTION_CHARS,
968
+ ): string | undefined {
969
+ let suggestion = rawSuggestion.replace(/\r/g, "");
970
+ if (!suggestion.trim()) return undefined;
971
+ if (/^<NO_COMPLETION>$/i.test(suggestion.trim())) return undefined;
972
+
973
+ suggestion = stripCodeFences(suggestion);
974
+ suggestion = suggestion.replace(/^(?:continuation|completion|suggestion)\s*:\s*/i, "");
975
+ suggestion = stripWrappingQuotes(suggestion);
976
+ suggestion = stripRepeatedDraftPrefix(draft, suggestion);
977
+ suggestion = suggestion.replace(/^\u200b+/, "");
978
+ suggestion = suggestion.replace(/\t/g, " ");
979
+ suggestion = stripRepeatedCurrentWordPrefix(draft, suggestion);
980
+ suggestion = suggestion.trimEnd();
981
+
982
+ if (!suggestion.trim()) return undefined;
983
+
984
+ suggestion = normalizeLeadingBoundarySpacing(draft, suggestion);
985
+ suggestion = maybePrefixSpace(draft, suggestion);
986
+ suggestion = truncateWithEllipsis(suggestion, maxChars);
987
+
988
+ if (!suggestion.trim()) return undefined;
989
+ return suggestion;
990
+ }
991
+
992
+ export function normalizePromptSuggestions(
993
+ draft: string,
994
+ rawResponse: string,
995
+ maxChars = DEFAULT_MAX_SUGGESTION_CHARS,
996
+ maxAlternatives = DEFAULT_MAX_ALTERNATIVES,
997
+ ): string[] {
998
+ const rawSuggestions = parseSuggestionResponse(rawResponse);
999
+ const normalized: string[] = [];
1000
+
1001
+ for (const rawSuggestion of rawSuggestions) {
1002
+ const suggestion = normalizePromptSuggestion(draft, rawSuggestion, maxChars);
1003
+ if (!suggestion) continue;
1004
+ if (normalized.includes(suggestion)) continue;
1005
+ normalized.push(suggestion);
1006
+ if (normalized.length >= maxAlternatives) break;
1007
+ }
1008
+
1009
+ return normalized;
1010
+ }
1011
+
1012
+ export function extractNextSuggestionChunk(suggestion: string): string | undefined {
1013
+ if (!suggestion) return undefined;
1014
+
1015
+ const match = /^(\s*)(\S+)(\s*)/s.exec(suggestion);
1016
+ if (!match) {
1017
+ return suggestion;
1018
+ }
1019
+
1020
+ const leading = match[1] ?? "";
1021
+ const token = match[2] ?? "";
1022
+ const trailing = match[3] ?? "";
1023
+
1024
+ let keptTrailing = "";
1025
+ if (trailing.startsWith("\n")) {
1026
+ const indent = /^\n([ \t]*)/.exec(trailing)?.[1] ?? "";
1027
+ keptTrailing = `\n${indent}`;
1028
+ } else if (/^[ \t]+/.test(trailing)) {
1029
+ keptTrailing = /^[ \t]+/.exec(trailing)?.[0] ?? "";
1030
+ }
1031
+
1032
+ return `${leading}${token}${keptTrailing}`;
1033
+ }
1034
+
1035
+ export function formatModelLabel(model: Model<Api> | undefined): string {
1036
+ if (!model) return "none";
1037
+ return `${model.provider}/${model.id}`;
1038
+ }