@drghaliasri/butex 7.0.4 → 7.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.
@@ -4485,6 +4485,104 @@ function applyDocument2Command(json, commandInput) {
4485
4485
  return toDocumentJson2(document2);
4486
4486
  }
4487
4487
 
4488
+ // src/document2/diff.ts
4489
+ var import_diff = require("diff");
4490
+ var MAX_TOTAL_TEXT_CHARS = 16e3;
4491
+ var MAX_CHANGED_CHUNK_CHARS = 4e3;
4492
+ var MAX_CONTEXT_CHUNK_CHARS = 600;
4493
+ var TRUNCATION_MARKER = "\n\u2026\n";
4494
+ var IDENTITY_KEYS = /* @__PURE__ */ new Set(["id", "inline_ids", "caption_inline_ids", "cell_inline_ids"]);
4495
+ function comparisonValue(value, key) {
4496
+ if (key && IDENTITY_KEYS.has(key)) {
4497
+ return void 0;
4498
+ }
4499
+ if (Array.isArray(value)) {
4500
+ return value.map((entry) => comparisonValue(entry));
4501
+ }
4502
+ if (value && typeof value === "object") {
4503
+ const result = {};
4504
+ for (const childKey of Object.keys(value).sort()) {
4505
+ const child = comparisonValue(value[childKey], childKey);
4506
+ if (child !== void 0) {
4507
+ result[childKey] = child;
4508
+ }
4509
+ }
4510
+ return result;
4511
+ }
4512
+ return value;
4513
+ }
4514
+ function normalizedComparisonDocument(document2) {
4515
+ return comparisonValue(toDocumentJson2(fromDocumentJson2(document2)));
4516
+ }
4517
+ function withoutFinalNewline(value) {
4518
+ return value.endsWith("\n") ? value.slice(0, -1) : value;
4519
+ }
4520
+ function shorten(text, limit, mode) {
4521
+ if (text.length <= limit) {
4522
+ return { text, truncated: false };
4523
+ }
4524
+ if (limit <= TRUNCATION_MARKER.length) {
4525
+ return { text: text.slice(0, Math.max(0, limit)), truncated: true };
4526
+ }
4527
+ const available = limit - TRUNCATION_MARKER.length;
4528
+ if (mode === "head") {
4529
+ return { text: `${text.slice(0, available)}${TRUNCATION_MARKER}`, truncated: true };
4530
+ }
4531
+ if (mode === "tail") {
4532
+ return { text: `${TRUNCATION_MARKER}${text.slice(text.length - available)}`, truncated: true };
4533
+ }
4534
+ const head = Math.ceil(available / 2);
4535
+ const tail = available - head;
4536
+ return {
4537
+ text: `${text.slice(0, head)}${TRUNCATION_MARKER}${text.slice(text.length - tail)}`,
4538
+ truncated: true
4539
+ };
4540
+ }
4541
+ function diffDocument2(before, after) {
4542
+ const beforeComparable = normalizedComparisonDocument(before);
4543
+ const afterComparable = normalizedComparisonDocument(after);
4544
+ const parts = (0, import_diff.diffJson)(beforeComparable, afterComparable);
4545
+ const changed = parts.some((part) => part.added === true || part.removed === true);
4546
+ if (!changed) {
4547
+ return { version: 1, changed: false, truncated: false, chunks: [] };
4548
+ }
4549
+ const chunks = [];
4550
+ let truncated = false;
4551
+ let totalTextChars = 0;
4552
+ const append = (kind, rawText, limit, mode) => {
4553
+ const cleaned = withoutFinalNewline(rawText);
4554
+ if (cleaned.length === 0) return;
4555
+ const perChunk = shorten(cleaned, limit, mode);
4556
+ truncated ||= perChunk.truncated;
4557
+ const remaining = MAX_TOTAL_TEXT_CHARS - totalTextChars;
4558
+ if (remaining <= 0) {
4559
+ truncated = true;
4560
+ return;
4561
+ }
4562
+ const withinBudget = shorten(perChunk.text, remaining, mode);
4563
+ truncated ||= withinBudget.truncated;
4564
+ if (withinBudget.text.length === 0) return;
4565
+ chunks.push({ kind, text: withinBudget.text });
4566
+ totalTextChars += withinBudget.text.length;
4567
+ };
4568
+ parts.forEach((part, index) => {
4569
+ if (part.added) {
4570
+ append("added", part.value, MAX_CHANGED_CHUNK_CHARS, "both");
4571
+ return;
4572
+ }
4573
+ if (part.removed) {
4574
+ append("removed", part.value, MAX_CHANGED_CHUNK_CHARS, "both");
4575
+ return;
4576
+ }
4577
+ const previousChanged = index > 0 && (parts[index - 1]?.added === true || parts[index - 1]?.removed === true);
4578
+ const nextChanged = index + 1 < parts.length && (parts[index + 1]?.added === true || parts[index + 1]?.removed === true);
4579
+ if (!previousChanged && !nextChanged) return;
4580
+ const mode = previousChanged && nextChanged ? "both" : previousChanged ? "head" : "tail";
4581
+ append("context", part.value, MAX_CONTEXT_CHUNK_CHARS, mode);
4582
+ });
4583
+ return { version: 1, changed: true, truncated, chunks };
4584
+ }
4585
+
4488
4586
  // src/document2/outline.ts
4489
4587
  var EXCERPT_LIMIT = 160;
4490
4588
  function normalizeExcerpt(value) {
@@ -5492,6 +5590,12 @@ function parseDocument2WorkerRequest(value) {
5492
5590
  if (!isObject4(value) || typeof value.action !== "string") {
5493
5591
  throw new Document2WorkerProtocolError("invalid_request", "Worker request requires an action");
5494
5592
  }
5593
+ if (value.action === "diff") {
5594
+ if (!("before" in value) || !("after" in value)) {
5595
+ throw new Document2WorkerProtocolError("invalid_request", "diff requires before and after documents");
5596
+ }
5597
+ return { action: "diff", before: value.before, after: value.after };
5598
+ }
5495
5599
  if (!("document" in value)) {
5496
5600
  throw new Document2WorkerProtocolError("invalid_request", "Worker request requires a document");
5497
5601
  }
@@ -5602,6 +5706,16 @@ function executeDocument2WorkerRequest(input) {
5602
5706
  if (request.action === "normalize") {
5603
5707
  return { ok: true, document: normalizeDocument(request.document) };
5604
5708
  }
5709
+ if (request.action === "diff") {
5710
+ try {
5711
+ return {
5712
+ ok: true,
5713
+ diff: diffDocument2(asDocument2Json(request.before), asDocument2Json(request.after))
5714
+ };
5715
+ } catch {
5716
+ throw new Document2CommandError("invalid_document", "Document JSON is invalid");
5717
+ }
5718
+ }
5605
5719
  if (request.action === "outline") {
5606
5720
  return { ok: true, outline: document2Outline(asDocument2Json(request.document)) };
5607
5721
  }
@@ -5759,6 +5873,9 @@ function actionForPath(pathname) {
5759
5873
  if (pathname === "/v1/document2/export") {
5760
5874
  return "export";
5761
5875
  }
5876
+ if (pathname === "/v1/document2/diff") {
5877
+ return "diff";
5878
+ }
5762
5879
  if (pathname === "/v1/document2/commands") {
5763
5880
  return "apply_command";
5764
5881
  }