pi-charset 0.1.7 → 0.1.9
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/CHANGELOG.md +27 -0
- package/CHANGELOG.zh-CN.md +27 -0
- package/dist/config.js +14 -0
- package/dist/diagnostics/incidents.js +436 -0
- package/dist/edit/diagnostics.js +27 -0
- package/dist/encoding/classify.js +26 -5
- package/dist/encoding/fast-codec.js +95 -44
- package/dist/faults.js +67 -0
- package/dist/mcp/server.js +303 -65
- package/dist/operations.js +172 -35
- package/dist/setup/adapters.js +625 -0
- package/dist/version.js +1 -1
- package/docs/MCP-AND-MULTI-AGENT.md +17 -2
- package/docs/zh-CN/MCP-AND-MULTI-AGENT.md +17 -2
- package/package.json +1 -1
- package/src/config.ts +13 -0
- package/src/diagnostics/incidents.ts +474 -0
- package/src/edit/diagnostics.ts +31 -0
- package/src/encoding/classify.ts +25 -5
- package/src/encoding/fast-codec.ts +81 -39
- package/src/faults.ts +165 -0
- package/src/mcp/server.ts +356 -65
- package/src/operations.ts +180 -37
- package/src/setup/adapters.ts +701 -0
- package/src/version.ts +1 -1
- package/tools/pi-charset-cli.mjs +233 -22
- package/tools/pi-charset-mcp.mjs +3 -1
|
@@ -185,13 +185,13 @@ function compileDbcs(encoding) {
|
|
|
185
185
|
break;
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
|
-
const bmpEncodable = new Uint8Array(8192);
|
|
189
188
|
let asciiIdentity = true;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const
|
|
194
|
-
|
|
189
|
+
let asciiEncodable = true;
|
|
190
|
+
const asciiBucket = encodeTable[0];
|
|
191
|
+
for (let cp = 0; cp < 128; cp++) {
|
|
192
|
+
const mb = asciiBucket !== undefined ? asciiBucket[cp] : UNASSIGNED;
|
|
193
|
+
if (mb !== cp)
|
|
194
|
+
asciiIdentity = false;
|
|
195
195
|
let ok = false;
|
|
196
196
|
if (mb >= 0 && (gb18030 || mb < 0x1000000)) {
|
|
197
197
|
ok = decodeMbCode(decodeTables, mb) === cp;
|
|
@@ -199,24 +199,16 @@ function compileDbcs(encoding) {
|
|
|
199
199
|
else if (mb === UNASSIGNED && gb18030) {
|
|
200
200
|
ok = gb18030CodePointEncodable(cp, gb18030, encodeTable);
|
|
201
201
|
}
|
|
202
|
-
if (ok)
|
|
203
|
-
bmpEncodable[cp >> 3] |= 1 << (cp & 7);
|
|
204
|
-
if (cp < 128 && mb !== cp)
|
|
205
|
-
asciiIdentity = false;
|
|
206
|
-
}
|
|
207
|
-
let asciiEncodable = true;
|
|
208
|
-
for (let cp = 0; cp < 128; cp++) {
|
|
209
|
-
if ((bmpEncodable[cp >> 3] & (1 << (cp & 7))) === 0) {
|
|
202
|
+
if (!ok)
|
|
210
203
|
asciiEncodable = false;
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
204
|
}
|
|
214
205
|
const compiled = {
|
|
215
206
|
dfa,
|
|
216
207
|
asciiFast,
|
|
217
208
|
gb18030,
|
|
209
|
+
decodeTables,
|
|
218
210
|
encodeTable,
|
|
219
|
-
bmpEncodable,
|
|
211
|
+
bmpEncodable: null,
|
|
220
212
|
hasEncodeSeq,
|
|
221
213
|
asciiEncodable,
|
|
222
214
|
bmpToMb: null,
|
|
@@ -226,6 +218,29 @@ function compileDbcs(encoding) {
|
|
|
226
218
|
compiledCache.set(encoding, compiled);
|
|
227
219
|
return compiled;
|
|
228
220
|
}
|
|
221
|
+
function getBmpEncodable(c) {
|
|
222
|
+
if (c.bmpEncodable)
|
|
223
|
+
return c.bmpEncodable;
|
|
224
|
+
const bits = new Uint8Array(8192);
|
|
225
|
+
const { encodeTable, decodeTables, gb18030 } = c;
|
|
226
|
+
for (let cp = 0; cp < 0x10000; cp++) {
|
|
227
|
+
if (cp >= 0xd800 && cp <= 0xdfff)
|
|
228
|
+
continue;
|
|
229
|
+
const bucket = encodeTable[cp >> 8];
|
|
230
|
+
const mb = bucket !== undefined ? bucket[cp & 0xff] : UNASSIGNED;
|
|
231
|
+
let ok = false;
|
|
232
|
+
if (mb >= 0 && (gb18030 || mb < 0x1000000)) {
|
|
233
|
+
ok = decodeMbCode(decodeTables, mb) === cp;
|
|
234
|
+
}
|
|
235
|
+
else if (mb === UNASSIGNED && gb18030) {
|
|
236
|
+
ok = gb18030CodePointEncodable(cp, gb18030, encodeTable);
|
|
237
|
+
}
|
|
238
|
+
if (ok)
|
|
239
|
+
bits[cp >> 3] |= 1 << (cp & 7);
|
|
240
|
+
}
|
|
241
|
+
c.bmpEncodable = bits;
|
|
242
|
+
return bits;
|
|
243
|
+
}
|
|
229
244
|
function getBmpEncodeTable(c) {
|
|
230
245
|
if (c.bmpToMb)
|
|
231
246
|
return c.bmpToMb;
|
|
@@ -256,35 +271,71 @@ export function fastExactRoundTrip(buf, encoding) {
|
|
|
256
271
|
const root = dfa[0];
|
|
257
272
|
let node = root;
|
|
258
273
|
const len = buf.length;
|
|
259
|
-
const asciiFast = c.asciiFast;
|
|
260
274
|
let icuSafe = c.icuDecoder !== null;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
275
|
+
if (c.asciiFast) {
|
|
276
|
+
let i = 0;
|
|
277
|
+
while (i < len) {
|
|
278
|
+
if (node === root) {
|
|
279
|
+
while (i < len && buf[i] < 0x80)
|
|
280
|
+
i++;
|
|
281
|
+
if (i >= len)
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
const b = buf[i];
|
|
285
|
+
const v = node[b];
|
|
286
|
+
if (v === DFA_VALID) {
|
|
287
|
+
node = root;
|
|
288
|
+
}
|
|
289
|
+
else if (v < 0) {
|
|
290
|
+
node = dfa[-v];
|
|
291
|
+
}
|
|
292
|
+
else if (v === DFA_VALID_ICU_DIFF) {
|
|
293
|
+
icuSafe = false;
|
|
294
|
+
node = root;
|
|
295
|
+
}
|
|
296
|
+
else if (v === DFA_GB18030 && c.gb18030 && i >= 3) {
|
|
297
|
+
icuSafe = false;
|
|
298
|
+
const ptr = (buf[i - 3] - 0x81) * 12600 + (buf[i - 2] - 0x30) * 1260 + (buf[i - 1] - 0x81) * 10 + (b - 0x30);
|
|
299
|
+
if (!gb18030PtrValid(ptr, c.gb18030, c.encodeTable))
|
|
300
|
+
return false;
|
|
301
|
+
node = root;
|
|
302
|
+
}
|
|
303
|
+
else if (v === DFA_FALLBACK) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
280
307
|
return false;
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
else if (v === DFA_FALLBACK) {
|
|
284
|
-
return null;
|
|
308
|
+
}
|
|
309
|
+
i++;
|
|
285
310
|
}
|
|
286
|
-
|
|
287
|
-
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
for (let i = 0; i < len; i++) {
|
|
314
|
+
const b = buf[i];
|
|
315
|
+
const v = node[b];
|
|
316
|
+
if (v === DFA_VALID) {
|
|
317
|
+
node = root;
|
|
318
|
+
}
|
|
319
|
+
else if (v < 0) {
|
|
320
|
+
node = dfa[-v];
|
|
321
|
+
}
|
|
322
|
+
else if (v === DFA_VALID_ICU_DIFF) {
|
|
323
|
+
icuSafe = false;
|
|
324
|
+
node = root;
|
|
325
|
+
}
|
|
326
|
+
else if (v === DFA_GB18030 && c.gb18030 && i >= 3) {
|
|
327
|
+
icuSafe = false;
|
|
328
|
+
const ptr = (buf[i - 3] - 0x81) * 12600 + (buf[i - 2] - 0x30) * 1260 + (buf[i - 1] - 0x81) * 10 + (b - 0x30);
|
|
329
|
+
if (!gb18030PtrValid(ptr, c.gb18030, c.encodeTable))
|
|
330
|
+
return false;
|
|
331
|
+
node = root;
|
|
332
|
+
}
|
|
333
|
+
else if (v === DFA_FALLBACK) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
288
339
|
}
|
|
289
340
|
}
|
|
290
341
|
if (node !== root)
|
|
@@ -431,7 +482,7 @@ export function fastCanEncodeAll(text, encoding) {
|
|
|
431
482
|
return null;
|
|
432
483
|
if (c.asciiEncodable && !NON_ASCII_STR.test(text))
|
|
433
484
|
return true;
|
|
434
|
-
const bits = c
|
|
485
|
+
const bits = getBmpEncodable(c);
|
|
435
486
|
const asciiOk = c.asciiEncodable;
|
|
436
487
|
const len = text.length;
|
|
437
488
|
for (let i = 0; i < len; i++) {
|
package/dist/faults.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Canonical typed fault contract shared across core I/O, edit/patch engines, grep, CLI, and MCP.
|
|
2
|
+
// Error classification always originates from typed codes at the source — never parsed from human-readable text.
|
|
3
|
+
export const REPORTABLE_FAULT_CODES = new Set([
|
|
4
|
+
"WRITE_VERIFY_FAILED",
|
|
5
|
+
"ROLLBACK_FAILED",
|
|
6
|
+
"BYTE_INVARIANT_FAILED",
|
|
7
|
+
"MCP_PROTOCOL_ERROR",
|
|
8
|
+
"CLIENT_STARTUP_FAILED",
|
|
9
|
+
"TOOL_BYPASS_DETECTED",
|
|
10
|
+
"LOCK_INVARIANT_FAILED",
|
|
11
|
+
"GIT_INDEX_INVARIANT_FAILED",
|
|
12
|
+
"SEARCH_ENGINE_FAILED",
|
|
13
|
+
"INTERNAL",
|
|
14
|
+
]);
|
|
15
|
+
export class CharsetFault extends Error {
|
|
16
|
+
fault;
|
|
17
|
+
constructor(message, fault) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "CharsetFault";
|
|
20
|
+
this.fault = Object.freeze({ ...fault });
|
|
21
|
+
}
|
|
22
|
+
get code() {
|
|
23
|
+
return this.fault.code;
|
|
24
|
+
}
|
|
25
|
+
get outcome() {
|
|
26
|
+
return this.fault.outcome;
|
|
27
|
+
}
|
|
28
|
+
get reportable() {
|
|
29
|
+
return REPORTABLE_FAULT_CODES.has(this.fault.code);
|
|
30
|
+
}
|
|
31
|
+
toMcpEnvelope() {
|
|
32
|
+
const f = this.fault;
|
|
33
|
+
return {
|
|
34
|
+
status: "failed",
|
|
35
|
+
error: {
|
|
36
|
+
code: f.code,
|
|
37
|
+
category: f.category,
|
|
38
|
+
phase: f.phase,
|
|
39
|
+
retryable: f.retryable,
|
|
40
|
+
},
|
|
41
|
+
applied: f.applied ?? false,
|
|
42
|
+
verified: f.verified ?? false,
|
|
43
|
+
committed: f.committed ?? false,
|
|
44
|
+
outcome: f.outcome,
|
|
45
|
+
actionHint: f.actionHint,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export function isCharsetFault(err) {
|
|
50
|
+
return err instanceof CharsetFault;
|
|
51
|
+
}
|
|
52
|
+
export function toMcpErrorEnvelope(fault) {
|
|
53
|
+
return {
|
|
54
|
+
status: "failed",
|
|
55
|
+
error: {
|
|
56
|
+
code: fault.code,
|
|
57
|
+
category: fault.category,
|
|
58
|
+
phase: fault.phase,
|
|
59
|
+
retryable: fault.retryable,
|
|
60
|
+
},
|
|
61
|
+
applied: fault.applied ?? false,
|
|
62
|
+
verified: fault.verified ?? false,
|
|
63
|
+
committed: fault.committed ?? false,
|
|
64
|
+
outcome: fault.outcome,
|
|
65
|
+
actionHint: fault.actionHint,
|
|
66
|
+
};
|
|
67
|
+
}
|