@ai-matrx/content-ir 0.18.5 → 0.19.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/dist/source.js CHANGED
@@ -268,12 +268,6 @@ function fenceOpenerOf(content) {
268
268
  return null;
269
269
  }
270
270
  var FenceReader = class {
271
- constructor(opener, options = {}) {
272
- this.opener = opener;
273
- this.isJson = opener.char === "`" && opener.lang === "json";
274
- this.nesting = opener.char === "`" && (options.nesting ?? fenceNestsInnerFences(opener.lang));
275
- this.tildeCloser = opener.char === "~" ? new RegExp(`^ {0,3}~{${opener.ticks},}[ \\t]*$`) : null;
276
- }
277
271
  opener;
278
272
  state = { inString: false, escaped: false };
279
273
  depth = 0;
@@ -282,6 +276,12 @@ var FenceReader = class {
282
276
  isJson;
283
277
  nesting;
284
278
  tildeCloser;
279
+ constructor(opener, options = {}) {
280
+ this.opener = opener;
281
+ this.isJson = opener.char === "`" && opener.lang === "json";
282
+ this.nesting = opener.char === "`" && (options.nesting ?? fenceNestsInnerFences(opener.lang));
283
+ this.tildeCloser = opener.char === "~" ? new RegExp(`^ {0,3}~{${opener.ticks},}[ \\t]*$`) : null;
284
+ }
285
285
  /** How many nested fences are open right now. */
286
286
  get nestedDepth() {
287
287
  return this.depth;
@@ -899,9 +899,6 @@ function isEscapedBacktick(line, offset) {
899
899
  return slashes % 2 === 1;
900
900
  }
901
901
  var XmlContainerTracker = class {
902
- constructor(rootTag) {
903
- this.rootTag = rootTag;
904
- }
905
902
  rootTag;
906
903
  depth = 0;
907
904
  inComment = false;
@@ -909,6 +906,9 @@ var XmlContainerTracker = class {
909
906
  inlineTicks = 0;
910
907
  /** The open fence, read by THE one closer (source/code-ranges.ts). */
911
908
  fence = null;
909
+ constructor(rootTag) {
910
+ this.rootTag = rootTag;
911
+ }
912
912
  consumeLine(line, startOffset = 0) {
913
913
  if (this.fence) {
914
914
  if (this.fence.feed(line)) this.fence = null;
@@ -1120,6 +1120,23 @@ function isSplitterTreeLine(line) {
1120
1120
  }
1121
1121
  var MEDIA_REF_PREFIXES = ["[Image URL:", "[Video URL:", "[Audio URL:"];
1122
1122
  var Tokenizer = class {
1123
+ text;
1124
+ streaming;
1125
+ meter;
1126
+ n;
1127
+ lineStarts = [];
1128
+ contentEnds = [];
1129
+ lineEnds = [];
1130
+ blocks = [];
1131
+ blockCache = /* @__PURE__ */ new Map();
1132
+ /** Lazily built: for line i, first line j >= i where brace net from i drops to <= 0. */
1133
+ braceStop = null;
1134
+ braceNet = null;
1135
+ /** Every occurrence offset of each needle searched so far (overlapping, like indexOf). */
1136
+ occurrences = /* @__PURE__ */ new Map();
1137
+ /** `$$` opener → closer for the pairs the core renders as math. */
1138
+ mathPairs;
1139
+ finder = (needle, from) => this.next(needle, from);
1123
1140
  constructor(text, streaming = false, meter = void 0) {
1124
1141
  this.text = text;
1125
1142
  this.streaming = streaming;
@@ -1166,23 +1183,6 @@ var Tokenizer = class {
1166
1183
  this.lineEnds.push(text.length);
1167
1184
  }
1168
1185
  }
1169
- text;
1170
- streaming;
1171
- meter;
1172
- n;
1173
- lineStarts = [];
1174
- contentEnds = [];
1175
- lineEnds = [];
1176
- blocks = [];
1177
- blockCache = /* @__PURE__ */ new Map();
1178
- /** Lazily built: for line i, first line j >= i where brace net from i drops to <= 0. */
1179
- braceStop = null;
1180
- braceNet = null;
1181
- /** Every occurrence offset of each needle searched so far (overlapping, like indexOf). */
1182
- occurrences = /* @__PURE__ */ new Map();
1183
- /** `$$` opener → closer for the pairs the core renders as math. */
1184
- mathPairs;
1185
- finder = (needle, from) => this.next(needle, from);
1186
1186
  /** Streaming: where provisional text starts (a pending `$$`), or +∞. */
1187
1187
  provisionalFrom = Number.POSITIVE_INFINITY;
1188
1188
  /** Streaming: the text ends in a lone CR whose LF has not arrived yet. */
@@ -1239,10 +1239,13 @@ var Tokenizer = class {
1239
1239
  /**
1240
1240
  * Streaming: an open block that may still turn back into prose — a `$$`
1241
1241
  * pair whose closer is on the open last line (prose after it makes it
1242
- * inline), or a JSON head whose braces have not balanced (it may never parse).
1242
+ * inline), a JSON head whose braces have not balanced (it may never parse),
1243
+ * or any opener that is itself still arriving on the open last line.
1243
1244
  */
1244
1245
  mayDissolve(block) {
1245
- return this.streaming && !block.complete && (block.type === "math_block" || block.type === "json");
1246
+ if (!this.streaming || block.complete) return false;
1247
+ if (block.type === "math_block" || block.type === "json") return true;
1248
+ return this.isOpenTail(this.lineOf(block.start));
1246
1249
  }
1247
1250
  /** Streaming: paragraph ends where an undecided, dissolvable block cut them short. */
1248
1251
  undecidedBreaks = /* @__PURE__ */ new Set();
@@ -1263,6 +1266,12 @@ var Tokenizer = class {
1263
1266
  }
1264
1267
  return lo;
1265
1268
  }
1269
+ /** How many whitespace characters open the text at `from` (up to `to`). */
1270
+ leadingSpace(from, to) {
1271
+ let k = from;
1272
+ while (k < to && /\s/.test(this.text[k])) k++;
1273
+ return k - from;
1274
+ }
1266
1275
  isBlank(from, to) {
1267
1276
  return !NOT_WHITESPACE.test(this.text.slice(from, to));
1268
1277
  }
@@ -1287,6 +1296,10 @@ var Tokenizer = class {
1287
1296
  break;
1288
1297
  }
1289
1298
  const island = this.detectBlock(pos, li, true);
1299
+ if (island && pos + this.leadingSpace(pos, this.contentEnds[li]) >= this.provisionalFrom) {
1300
+ this.push({ kind: "prose", start: pos, end: this.n, island: null, inlines: [] });
1301
+ break;
1302
+ }
1290
1303
  if (island) {
1291
1304
  this.push({ kind: "island", start: island.start, end: island.end, island, inlines: [] });
1292
1305
  pos = island.end;
@@ -1316,12 +1329,6 @@ var Tokenizer = class {
1316
1329
  const last = this.lineStarts.length - 1;
1317
1330
  if (last < 0) return;
1318
1331
  if (this.isOpenTail(last)) {
1319
- const lineStart = this.lineStarts[last];
1320
- for (const block2 of this.blocks) {
1321
- if (block2.kind === "prose" && block2.end > lineStart) {
1322
- block2.inlines = block2.inlines.filter((inline) => inline.end <= lineStart);
1323
- }
1324
- }
1325
1332
  let b = this.blocks.length - 1;
1326
1333
  while (b >= 0 && this.blocks[b].kind === "gap") b--;
1327
1334
  const block = this.blocks[b];
@@ -1347,36 +1354,55 @@ var Tokenizer = class {
1347
1354
  * region…) is never a tree line nor a root label, so it stops the walk.
1348
1355
  */
1349
1356
  treeRootHoldFrom() {
1350
- const claimable = (k2) => {
1357
+ const claimFrom = (k2) => {
1358
+ const lineStart = this.lineStarts[k2];
1359
+ const lineEnd = this.contentEnds[k2];
1360
+ let pos = lineStart;
1351
1361
  for (; ; ) {
1352
- const block = this.blockAt(this.lineStarts[k2]);
1353
- if (!block || block.kind !== "island" || block.island?.fromParagraph === true) return true;
1354
- const opened = this.lineOf(block.start);
1355
- if (this.lineStarts[opened] === block.start || opened === k2) return false;
1356
- k2 = opened;
1362
+ const block = this.blockAt(pos);
1363
+ if (!block || block.kind !== "island" || !block.island) return pos;
1364
+ if (block.island.fromParagraph === true || this.mayDissolve(block.island)) return pos;
1365
+ if (block.start < lineStart) {
1366
+ const opened = this.lineOf(block.start);
1367
+ if (this.lineStarts[opened] !== block.start && claimFrom(opened) !== -1) return pos;
1368
+ if (block.end >= lineEnd) return -1;
1369
+ pos = block.end;
1370
+ continue;
1371
+ }
1372
+ if (block.end >= lineEnd) return -1;
1373
+ pos = block.end;
1357
1374
  }
1358
1375
  };
1359
1376
  let k = this.lineCount - 1;
1360
1377
  let start = this.lineCount;
1378
+ let startPos = this.n;
1361
1379
  if (k >= 0 && this.isOpenTail(k)) {
1362
- if (!claimable(k)) return this.n;
1380
+ const at = claimFrom(k);
1381
+ if (at === -1) return this.n;
1363
1382
  start = k;
1383
+ startPos = at;
1364
1384
  k--;
1365
1385
  }
1366
- for (; k >= 0 && claimable(k); k--) {
1386
+ for (; k >= 0; k--) {
1367
1387
  this.tick(1);
1388
+ const at = claimFrom(k);
1389
+ if (at === -1) break;
1368
1390
  if (this.lineIsBlank(k)) continue;
1369
1391
  if (!TREE_GLYPHS.test(this.lineText(k))) break;
1370
1392
  start = k;
1393
+ startPos = at;
1371
1394
  }
1372
- let root = start;
1373
- for (let j = start - 1; j >= 0 && claimable(j); j--) {
1395
+ let rootPos = startPos;
1396
+ for (let j = start - 1; j >= 0; j--) {
1397
+ const at = claimFrom(j);
1398
+ if (at === -1) break;
1374
1399
  const trimmed = this.lineText(j).trim();
1375
1400
  this.tick(trimmed.length + 1);
1376
1401
  if (!trimmed || isSplitterTreeLine(trimmed) || /^#{1,6}\s/.test(trimmed)) break;
1377
- root = j;
1402
+ rootPos = at;
1403
+ if (at > this.lineStarts[j]) break;
1378
1404
  }
1379
- return root < this.lineCount ? this.lineStarts[root] : this.n;
1405
+ return rootPos;
1380
1406
  }
1381
1407
  /** The block (so far) containing offset `pos`, or null. */
1382
1408
  blockAt(pos) {
@@ -1908,7 +1934,9 @@ var Tokenizer = class {
1908
1934
  const block = this.detectBlock(this.lineStarts[k], k, false);
1909
1935
  if (block) {
1910
1936
  breakLine = k;
1911
- if (this.mayDissolve(block)) this.undecidedBreaks.add(end);
1937
+ if (this.mayDissolve(block) || this.streaming && block.start >= this.provisionalFrom) {
1938
+ this.undecidedBreaks.add(end);
1939
+ }
1912
1940
  break;
1913
1941
  }
1914
1942
  end = this.contentEnds[k];
@@ -2146,6 +2174,10 @@ var Tokenizer = class {
2146
2174
  const closer = `</${name}>`;
2147
2175
  const close = this.next(closer, tagEnd);
2148
2176
  const blockType = isAttr ? "xml_attr" : "xml_region";
2177
+ if (close === -1 && isThinking && !this.streaming) {
2178
+ i = tagEnd;
2179
+ continue;
2180
+ }
2149
2181
  if (close === -1) return stop(this.island(blockType, i, this.n, false, { tag: name }));
2150
2182
  const end = close + closer.length;
2151
2183
  if (end > pe) return stop(this.spill(blockType, i, end, pe, { tag: name }));
@@ -2249,12 +2281,12 @@ function blockAt(blocks, offset) {
2249
2281
 
2250
2282
  // source/splice.ts
2251
2283
  var SourceSpliceError = class extends Error {
2284
+ code;
2252
2285
  constructor(code, message) {
2253
2286
  super(message);
2254
2287
  this.code = code;
2255
2288
  this.name = "SourceSpliceError";
2256
2289
  }
2257
- code;
2258
2290
  };
2259
2291
  function blockEdit(block, text) {
2260
2292
  return { start: block.start, end: block.end, text };