@each1024/pi-mini-mode 2.0.18 → 2.0.19

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.
@@ -8,8 +8,20 @@ import { commitImageCursor } from "./image-placement.ts";
8
8
  const parser = new Marked();
9
9
  export interface AssistantImageSpan { start: number; end: number; path: string }
10
10
 
11
+ const spansCache = new Map<string, AssistantImageSpan[]>();
12
+ const placeholdersCache = new Map<string, string>();
13
+ const IMAGE_CACHE_MAX = 800;
14
+
15
+ export function clearAssistantImageCaches(): void {
16
+ spansCache.clear();
17
+ placeholdersCache.clear();
18
+ }
19
+
11
20
  /** Parse image syntax before bare paths, retaining source offsets for placement. */
12
21
  export function assistantImageSpans(source: string, cwd: string, allowMissing = false): AssistantImageSpan[] {
22
+ const cacheKey = `${allowMissing ? 1 : 0}|${cwd}|${source}`;
23
+ const cached = spansCache.get(cacheKey);
24
+ if (cached !== undefined) return cached;
13
25
  const spans: AssistantImageSpan[] = [];
14
26
  let cursor = 0;
15
27
  let offset = 0;
@@ -40,20 +52,35 @@ export function assistantImageSpans(source: string, cwd: string, allowMissing =
40
52
  spans.push({ start: match.start, end: match.end, path: localPath(match.path, cwd) });
41
53
  }
42
54
  }
43
- return spans.filter(span => existsSync(span.path) || (allowMissing
55
+ const result = spans.filter(span => existsSync(span.path) || (allowMissing
44
56
  && !/[*?]/.test(span.path)
45
57
  && /^(?:!\[|\/|~\/|\.{1,2}\/|[a-z]:[\\/])/i.test(source.slice(span.start, span.end))))
46
58
  .sort((a, b) => a.start - b.start);
59
+ if (spansCache.size >= IMAGE_CACHE_MAX) {
60
+ const first = spansCache.keys().next().value;
61
+ if (first !== undefined) spansCache.delete(first);
62
+ }
63
+ spansCache.set(cacheKey, result);
64
+ return result;
47
65
  }
48
66
 
49
67
  /** Keep explicit image references compact even after clipboard files have expired. */
50
68
  export function imagePlaceholders(source: string, cwd: string): string {
69
+ const cacheKey = `${cwd}|${source}`;
70
+ const cached = placeholdersCache.get(cacheKey);
71
+ if (cached !== undefined) return cached;
72
+ let out = source;
51
73
  const spans = assistantImageSpans(source, cwd, true);
52
74
  for (let index = spans.length - 1; index >= 0; index--) {
53
75
  const span = spans[index];
54
- source = source.slice(0, span.start) + fileLink(` 图片 ${index + 1}`, span.path, false) + source.slice(span.end);
76
+ out = out.slice(0, span.start) + fileLink(` 图片 ${index + 1}`, span.path, false) + out.slice(span.end);
77
+ }
78
+ if (placeholdersCache.size >= IMAGE_CACHE_MAX) {
79
+ const first = placeholdersCache.keys().next().value;
80
+ if (first !== undefined) placeholdersCache.delete(first);
55
81
  }
56
- return source;
82
+ placeholdersCache.set(cacheKey, out);
83
+ return out;
57
84
  }
58
85
 
59
86
  /** Load once outside render, then reuse Pi's Image IDs and complete height rows. */
package/lib/file-links.ts CHANGED
@@ -65,14 +65,24 @@ export function filePaths(text: string) {
65
65
  // BEL survives Markdown's backslash-escape parsing, unlike the ST terminator.
66
66
  export const fileLink = (text: string, path: string, underline = true) => `\x1b]8;;${pathToFileURL(path).href}\x07${underline ? `\x1b[4m${text}\x1b[24m` : text}\x1b]8;;\x07`;
67
67
 
68
+ const linkToolFilesCache = new Map<string, string>();
69
+ const FILE_LINK_CACHE_MAX = 800;
70
+
71
+ export function clearFileLinkCaches(): void {
72
+ linkToolFilesCache.clear();
73
+ }
74
+
68
75
  /** Shorten only visible tool labels; OSC 8 targets retain the complete original path. */
69
76
  export function linkToolFiles(text: string, cwd: string, compact: boolean): string {
77
+ const hyperlinks = inputCapabilities().hyperlinks;
78
+ const cacheKey = `${compact ? 1 : 0}|${hyperlinks ? 1 : 0}|${cwd}|${text}`;
79
+ const cached = linkToolFilesCache.get(cacheKey);
80
+ if (cached !== undefined) return cached;
70
81
  const shorten = (label: string) => compact ? label
71
82
  .replace(/\/var\/folders\/[^\s]*\/otty-paste\//g, "…/otty-paste/")
72
83
  .replace(/node_modules\/@earendil-works\//g, "…/") : label;
73
- const hyperlinks = inputCapabilities().hyperlinks;
74
84
  // Image chips already carry a complete target; never shorten or relink it.
75
- return text.split(/(\x1b\]8;[^\x07]*\x07[\s\S]*?\x1b\]8;;\x07)/g).map((part, index) => {
85
+ const out = text.split(/(\x1b\]8;[^\x07]*\x07[\s\S]*?\x1b\]8;;\x07)/g).map((part, index) => {
76
86
  if (index % 2) return part;
77
87
  if (!hyperlinks) return shorten(part);
78
88
  // A path tool's entire argument can contain spaces or punctuation without quotes.
@@ -89,6 +99,12 @@ export function linkToolFiles(text: string, cwd: string, compact: boolean): stri
89
99
  }
90
100
  return result + shorten(part.slice(offset));
91
101
  }).join("");
102
+ if (linkToolFilesCache.size >= FILE_LINK_CACHE_MAX) {
103
+ const first = linkToolFilesCache.keys().next().value;
104
+ if (first !== undefined) linkToolFilesCache.delete(first);
105
+ }
106
+ linkToolFilesCache.set(cacheKey, out);
107
+ return out;
92
108
  }
93
109
 
94
110
  /** Display-only: normalize local targets without changing persisted messages or remote URLs. */