pi-charset 0.1.6 → 0.1.7

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.
Files changed (42) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/CHANGELOG.zh-CN.md +18 -0
  3. package/dist/config.js +943 -0
  4. package/dist/edit/apply-patch-core.js +274 -0
  5. package/dist/edit/diagnostics.js +46 -0
  6. package/dist/edit/hashline.js +400 -0
  7. package/dist/edit/patch.js +634 -0
  8. package/dist/edit/paths.js +48 -0
  9. package/dist/edit/plan.js +90 -0
  10. package/dist/encoding/classify.js +430 -0
  11. package/dist/encoding/common-scripts.js +58 -0
  12. package/dist/encoding/converter.js +311 -0
  13. package/dist/encoding/fast-codec.js +476 -0
  14. package/dist/encoding/file-stamp.js +32 -0
  15. package/dist/encoding/line-endings.js +118 -0
  16. package/dist/encoding/mime.js +88 -0
  17. package/dist/encoding/stream-transcode.js +287 -0
  18. package/dist/grep-core.js +730 -0
  19. package/dist/journal.js +494 -0
  20. package/dist/maintenance.js +333 -0
  21. package/dist/mcp/server.js +866 -0
  22. package/dist/notify.js +60 -0
  23. package/dist/operations.js +1021 -0
  24. package/dist/path-key.js +10 -0
  25. package/dist/read-view.js +927 -0
  26. package/dist/resolve.js +325 -0
  27. package/dist/version.js +2 -0
  28. package/docs/MCP-AND-MULTI-AGENT.md +27 -19
  29. package/docs/zh-CN/MCP-AND-MULTI-AGENT.md +27 -19
  30. package/package.json +14 -2
  31. package/src/edit/apply-patch-core.ts +32 -3
  32. package/src/grep-core.ts +50 -8
  33. package/src/grep.ts +9 -1
  34. package/src/index.ts +8 -5
  35. package/src/journal.ts +167 -42
  36. package/src/mcp/server.ts +435 -163
  37. package/src/operations.ts +197 -68
  38. package/src/read-view.ts +111 -8
  39. package/src/shell-guard.ts +38 -1
  40. package/src/version.ts +1 -1
  41. package/tools/pi-charset-cli.mjs +453 -167
  42. package/tools/pi-charset-mcp.mjs +78 -41
@@ -0,0 +1,311 @@
1
+ import iconv from "iconv-lite";
2
+ import { fastCanEncodeAll, fastDecodeDbcs, fastEncodeDbcs } from "./fast-codec.js";
3
+ export class UnsupportedEncodingError extends Error {
4
+ label;
5
+ constructor(label) {
6
+ super(`不支持的编码 "${label}":iconv-lite 未收录该编码。` +
7
+ `可用编码示例:UTF-8 / GB18030 / GBK / GB2312 / Big5 / Shift_JIS / EUC-KR / EUC-JP / ` +
8
+ `ISO-8859-1 / windows-1252 / UTF-16LE / UTF-16BE / UTF-32LE。`);
9
+ this.label = label;
10
+ this.name = "UnsupportedEncodingError";
11
+ }
12
+ }
13
+ /** Multi-byte CJK encodings eligible for lossless byte round-trip classification (single-byte encodings excluded). */
14
+ export const AUTO_CJK_CANDIDATES = [
15
+ "GB18030",
16
+ "GBK",
17
+ "GB2312",
18
+ "Big5",
19
+ "Shift_JIS",
20
+ "EUC-KR",
21
+ "EUC-JP",
22
+ ];
23
+ const ALIASES = {
24
+ UTF8: "UTF-8",
25
+ "UTF-8": "UTF-8",
26
+ "UNICODE-1-1-UTF-8": "UTF-8",
27
+ "UTF-16": "UTF-16LE",
28
+ UTF16: "UTF-16LE",
29
+ "UTF-16LE": "UTF-16LE",
30
+ UCS2LE: "UTF-16LE",
31
+ "UTF-16BE": "UTF-16BE",
32
+ UCS2BE: "UTF-16BE",
33
+ "UTF-32LE": "UTF-32LE",
34
+ "UTF-32BE": "UTF-32BE",
35
+ GB18030: "GB18030",
36
+ GBK: "GBK",
37
+ CP936: "GBK",
38
+ MS936: "GBK",
39
+ "X-CP936": "GBK",
40
+ GB2312: "GB2312",
41
+ "EUC-CN": "GB2312",
42
+ CP937: "GB2312",
43
+ BIG5: "Big5",
44
+ "BIG-5": "Big5",
45
+ CP950: "Big5",
46
+ "MS-950": "Big5",
47
+ "SHIFT-JIS": "Shift_JIS",
48
+ SJIS: "Shift_JIS",
49
+ CP932: "Shift_JIS",
50
+ "X-SJIS": "Shift_JIS",
51
+ "EUC-KR": "EUC-KR",
52
+ CP949: "EUC-KR",
53
+ UHC: "EUC-KR",
54
+ "EUC-JP": "EUC-JP",
55
+ "EUCJP-MS": "EUC-JP",
56
+ "ISO-8859-1": "ISO-8859-1",
57
+ LATIN1: "ISO-8859-1",
58
+ "ISO8859-1": "ISO-8859-1",
59
+ CP819: "ISO-8859-1",
60
+ "ISO-8859-2": "ISO-8859-2",
61
+ "ISO-8859-5": "ISO-8859-5",
62
+ "ISO-8859-7": "ISO-8859-7",
63
+ "ISO-8859-9": "ISO-8859-9",
64
+ "ISO-8859-15": "ISO-8859-15",
65
+ "WINDOWS-1250": "windows-1250",
66
+ "WINDOWS-1251": "windows-1251",
67
+ "WINDOWS-1252": "windows-1252",
68
+ CP1252: "windows-1252",
69
+ CP1251: "windows-1251",
70
+ CP1250: "windows-1250",
71
+ KOI8R: "KOI8-R",
72
+ KOI8U: "KOI8-U",
73
+ CP437: "CP437",
74
+ CP850: "CP850",
75
+ CP852: "CP852",
76
+ MACINTOSH: "MACINTOSH",
77
+ IBM866: "CP866",
78
+ ASCII: "US-ASCII",
79
+ "US-ASCII": "US-ASCII",
80
+ };
81
+ export function normalizeEncoding(label) {
82
+ const raw = String(label ?? "").trim();
83
+ if (!raw)
84
+ return "UTF-8";
85
+ const key = raw.toUpperCase().replace(/_/g, "-").replace(/\s+/g, "");
86
+ const alias = ALIASES[key] ?? ALIASES[key.replace(/-/g, "")];
87
+ const candidate = alias ?? raw;
88
+ if (!iconv.encodingExists(candidate)) {
89
+ const loose = candidate.replace(/-/g, "");
90
+ if (iconv.encodingExists(loose))
91
+ return loose;
92
+ throw new UnsupportedEncodingError(raw);
93
+ }
94
+ return candidate;
95
+ }
96
+ export function isSupportedEncoding(label) {
97
+ try {
98
+ normalizeEncoding(label);
99
+ return true;
100
+ }
101
+ catch {
102
+ return false;
103
+ }
104
+ }
105
+ const GB_RE = /^(GB18030|GBK|GB2312|EUC-?CN|CP936|CP937|GBK-?EUIPO)$/i;
106
+ export function isGBEncoding(encoding) {
107
+ const n = safeNormalize(encoding);
108
+ return GB_RE.test(n) || n.toUpperCase().includes("GB");
109
+ }
110
+ function safeNormalize(label) {
111
+ try {
112
+ return normalizeEncoding(label);
113
+ }
114
+ catch {
115
+ return label;
116
+ }
117
+ }
118
+ /** Single-byte encodings round-trip arbitrary byte sequences and require explicit force: true in configuration. */
119
+ const SINGLE_BYTE_RE = /^(ISO-8859-\d+|ISO-8859-1[0-6]|windows-125\d|windows-1250|CP125\d|CP437|CP850|CP852|CP855|CP866|CP874|KOI8-[RU]|MACINTOSH|IBM\d{3,4}|US-ASCII|LATIN\d)$/i;
120
+ export function isSingleByteEncoding(encoding) {
121
+ const n = safeNormalize(encoding);
122
+ return SINGLE_BYTE_RE.test(n) || SINGLE_BYTE_RE.test(n.replace(/-/g, ""));
123
+ }
124
+ /** Stateful escape-sequence encodings cannot be verified via byte-exact round-trip. */
125
+ const STATEFUL_RE = /^(ISO-2022-|EBCDIC|CS|\d{4}$)/i;
126
+ export function isStatefulEncoding(encoding) {
127
+ return STATEFUL_RE.test(safeNormalize(encoding).toUpperCase());
128
+ }
129
+ export function decodeToUtf8(buffer, encoding) {
130
+ if (encoding === "UTF-8") {
131
+ // Match iconv-lite's default stripBOM: true for UTF-8 without going through JS decoder overhead.
132
+ const start = buffer.length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf ? 3 : 0;
133
+ return buffer.toString("utf8", start);
134
+ }
135
+ const fast = fastDecodeDbcs(buffer, encoding);
136
+ if (fast !== null)
137
+ return fast;
138
+ return iconv.decode(buffer, encoding);
139
+ }
140
+ export function encodeFromUtf8(text, encoding) {
141
+ if (encoding === "UTF-8")
142
+ return Buffer.from(text, "utf8");
143
+ const fast = fastEncodeDbcs(text, encoding);
144
+ if (fast !== null)
145
+ return fast;
146
+ return iconv.encode(text, encoding);
147
+ }
148
+ /** Check whether a string contains only well-formed Unicode (rejecting lone UTF-16 surrogates). */
149
+ export function isWellFormed(s) {
150
+ return s.isWellFormed();
151
+ }
152
+ /** Fast check for whether a single character (or surrogate pair) can be losslessly represented in target encoding. */
153
+ export function canEncodeChar(ch, encoding) {
154
+ if (!isWellFormed(ch))
155
+ return false;
156
+ const fast = fastCanEncodeAll(ch, encoding);
157
+ if (fast !== null)
158
+ return fast;
159
+ try {
160
+ const b = iconv.encode(ch, encoding);
161
+ return iconv.decode(b, encoding, { stripBOM: false }) === ch;
162
+ }
163
+ catch {
164
+ return false;
165
+ }
166
+ }
167
+ /** Check whether all code points in text can be losslessly represented in the target encoding via round-trip verification. */
168
+ export function canEncodeAll(text, encoding) {
169
+ if (!isWellFormed(text))
170
+ return false;
171
+ const fast = fastCanEncodeAll(text, encoding);
172
+ if (fast !== null)
173
+ return fast;
174
+ try {
175
+ const buf = iconv.encode(text, encoding);
176
+ return iconv.decode(buf, encoding, { stripBOM: false }) === text;
177
+ }
178
+ catch {
179
+ return false;
180
+ }
181
+ }
182
+ /** Collect code points in text that cannot be losslessly encoded in the target encoding. */
183
+ export function unmappableChars(text, encoding) {
184
+ const bad = [];
185
+ const seen = new Set();
186
+ for (const ch of text) {
187
+ if (seen.has(ch))
188
+ continue;
189
+ seen.add(ch);
190
+ if (!canEncodeChar(ch, encoding))
191
+ bad.push(ch);
192
+ }
193
+ return bad;
194
+ }
195
+ export function charListMessage(chars) {
196
+ return chars.map((c) => `${c} U+${c.codePointAt(0).toString(16).toUpperCase().padStart(4, "0")}`).join(", ");
197
+ }
198
+ // Common LLM typographic characters and status symbols that frequently fail Gate 1 in legacy CJK/single-byte files.
199
+ const TYPO_SUGGESTIONS = {
200
+ 0x00a0: " ",
201
+ 0x200b: "",
202
+ 0x00a9: "(C)",
203
+ 0x00ae: "(R)",
204
+ 0x00b0: "deg",
205
+ 0x00b1: "+/-",
206
+ 0x00ab: '"',
207
+ 0x00bb: '"',
208
+ 0x00d7: "x",
209
+ 0x00f7: "/",
210
+ 0x2013: "-",
211
+ 0x2014: "--",
212
+ 0x2018: "'",
213
+ 0x2019: "'",
214
+ 0x201c: '"',
215
+ 0x201d: '"',
216
+ 0x2022: "-",
217
+ 0x2026: "...",
218
+ 0x2039: "'",
219
+ 0x203a: "'",
220
+ 0x2122: "(TM)",
221
+ 0x2190: "<-",
222
+ 0x2192: "->",
223
+ 0x21d2: "=>",
224
+ 0x2139: "[INFO]",
225
+ 0x221e: "inf",
226
+ 0x2248: "~=",
227
+ 0x2260: "!=",
228
+ 0x2264: "<=",
229
+ 0x2265: ">=",
230
+ 0x25aa: "*",
231
+ 0x25b8: ">",
232
+ 0x25ba: ">",
233
+ 0x2605: "*",
234
+ 0x2606: "*",
235
+ 0x26a0: "[WARN]",
236
+ 0x2713: "[OK]",
237
+ 0x2714: "[OK]",
238
+ 0x2705: "[OK]",
239
+ 0x2717: "[FAIL]",
240
+ 0x2718: "[FAIL]",
241
+ 0x274c: "[FAIL]",
242
+ 0x1f4a1: "[TIP]",
243
+ 0x1f4e6: "[PKG]",
244
+ 0x1f50d: "[SEARCH]",
245
+ 0x1f527: "[FIX]",
246
+ 0x1f680: "[LAUNCH]",
247
+ };
248
+ /** Collect unmappable code points with their first 1-based line:col offset and safe ASCII replacement hint (if known). */
249
+ export function unmappableDetails(text, encoding, max = 12) {
250
+ const out = [];
251
+ const seen = new Set();
252
+ let line = 1;
253
+ let col = 1;
254
+ for (const ch of text) {
255
+ if (ch === "\n") {
256
+ line++;
257
+ col = 1;
258
+ continue;
259
+ }
260
+ if (!seen.has(ch)) {
261
+ seen.add(ch);
262
+ if (!canEncodeChar(ch, encoding)) {
263
+ const cp = ch.codePointAt(0);
264
+ const codePoint = `U+${cp.toString(16).toUpperCase().padStart(4, "0")}`;
265
+ const suggestion = TYPO_SUGGESTIONS[cp];
266
+ out.push(suggestion !== undefined ? { char: ch, codePoint, line, col, suggestion } : { char: ch, codePoint, line, col });
267
+ if (out.length >= max)
268
+ break;
269
+ }
270
+ }
271
+ col++;
272
+ }
273
+ return out;
274
+ }
275
+ export function formatUnmappableDetails(details) {
276
+ return details
277
+ .map((d) => {
278
+ const hint = d.suggestion !== undefined ? ` (try ${JSON.stringify(d.suggestion)})` : "";
279
+ return `${d.char} ${d.codePoint} @L${d.line}:${d.col}${hint}`;
280
+ })
281
+ .join(", ");
282
+ }
283
+ /** Replace unmappable code points with \uXXXX escapes (surrogate pairs for astral code points). */
284
+ export function escapeUnmappable(text, encoding) {
285
+ if (canEncodeAll(text, encoding))
286
+ return text;
287
+ let out = "";
288
+ // Local to this conversion: repeated source characters need only one codec round trip.
289
+ const encodable = new Map();
290
+ for (const ch of text) {
291
+ let supported = encodable.get(ch);
292
+ if (supported === undefined) {
293
+ supported = canEncodeChar(ch, encoding);
294
+ encodable.set(ch, supported);
295
+ }
296
+ if (supported) {
297
+ out += ch;
298
+ continue;
299
+ }
300
+ const cp = ch.codePointAt(0);
301
+ if (cp > 0xffff) {
302
+ const hi = 0xd800 + ((cp - 0x10000) >> 10);
303
+ const lo = 0xdc00 + ((cp - 0x10000) & 0x3ff);
304
+ out += `\\u${hi.toString(16).padStart(4, "0")}\\u${lo.toString(16).padStart(4, "0")}`;
305
+ }
306
+ else {
307
+ out += `\\u${cp.toString(16).padStart(4, "0")}`;
308
+ }
309
+ }
310
+ return out;
311
+ }