@codelia/storage 0.1.35 → 0.1.36

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 (3) hide show
  1. package/dist/index.cjs +126 -11
  2. package/dist/index.js +126 -11
  3. package/package.json +3 -3
package/dist/index.cjs CHANGED
@@ -771,9 +771,74 @@ var SessionStateStoreImpl = class {
771
771
  var import_node_fs5 = require("fs");
772
772
  var import_node_path5 = __toESM(require("path"), 1);
773
773
  var DEFAULT_MAX_MATCHES = 50;
774
+ var MAX_LINE_LENGTH = 2e3;
775
+ var MAX_READ_BYTES = 50 * 1024;
776
+ var MAX_GREP_BYTES = 50 * 1024;
774
777
  var normalizeRefId = (refId) => refId.replace(/[^a-zA-Z0-9_-]/g, "_");
775
778
  var formatLineNumber = (lineNumber) => String(lineNumber).padStart(5, " ");
776
779
  var toLineNumbered = (lines, offset) => lines.map((line, index) => `${formatLineNumber(index + offset + 1)} ${line}`).join("\n");
780
+ var splitLongLine = (line) => {
781
+ if (line.length <= MAX_LINE_LENGTH) {
782
+ return [line];
783
+ }
784
+ const chunks = [];
785
+ for (let start = 0; start < line.length; start += MAX_LINE_LENGTH) {
786
+ chunks.push(line.slice(start, start + MAX_LINE_LENGTH));
787
+ }
788
+ return chunks;
789
+ };
790
+ var toDisplayLines = (physicalLines, options) => {
791
+ let wrapped = false;
792
+ let clipped = false;
793
+ const display = [];
794
+ for (const line of physicalLines) {
795
+ if (options.wrapLongLines) {
796
+ const chunks = splitLongLine(line);
797
+ if (chunks.length > 1) wrapped = true;
798
+ display.push(...chunks);
799
+ continue;
800
+ }
801
+ if (line.length > MAX_LINE_LENGTH) {
802
+ clipped = true;
803
+ display.push(`${line.slice(0, MAX_LINE_LENGTH)}...`);
804
+ continue;
805
+ }
806
+ display.push(line);
807
+ }
808
+ return { lines: display, wrapped, clipped };
809
+ };
810
+ var appendReadSuffix = (output, options) => {
811
+ let next = output;
812
+ if (options.truncatedByBytes || options.hasMoreLines) {
813
+ const reason = options.truncatedByBytes ? `Output truncated at ${MAX_READ_BYTES} bytes.` : "Output has more lines.";
814
+ next += `
815
+
816
+ ${reason} Use offset to read beyond line ${options.lastReadLine}.`;
817
+ }
818
+ if (options.wrapped) {
819
+ next += `
820
+
821
+ Long physical lines are wrapped at ${MAX_LINE_LENGTH} chars per display line. Wrapped chunks are display-only and may not match exact source text for edit.`;
822
+ }
823
+ if (options.clipped) {
824
+ next += `
825
+
826
+ Long physical lines are clipped at ${MAX_LINE_LENGTH} chars. Set wrap_long_lines=true to paginate full lines. Clipped output is display-only and may not match exact source text for edit.`;
827
+ }
828
+ return next;
829
+ };
830
+ var renderPhysicalSnippet = (physicalLines, start, end) => {
831
+ let wrapped = false;
832
+ const output = [];
833
+ for (let index = start; index < end; index += 1) {
834
+ const chunks = splitLongLine(physicalLines[index] ?? "");
835
+ if (chunks.length > 1) wrapped = true;
836
+ for (const chunk of chunks) {
837
+ output.push(`${formatLineNumber(index + 1)} ${chunk}`);
838
+ }
839
+ }
840
+ return { text: output.join("\n"), wrapped };
841
+ };
777
842
  var ToolOutputCacheStoreImpl = class {
778
843
  baseDir;
779
844
  constructor(options = {}) {
@@ -792,14 +857,37 @@ var ToolOutputCacheStoreImpl = class {
792
857
  async read(refId, options = {}) {
793
858
  const filePath = this.resolvePath(refId);
794
859
  const content = await import_node_fs5.promises.readFile(filePath, "utf8");
795
- const lines = content.split(/\r?\n/);
860
+ const physicalLines = content.split(/\r?\n/);
861
+ const wrapLongLines = options.wrap_long_lines ?? false;
862
+ const display = toDisplayLines(physicalLines, { wrapLongLines });
796
863
  const offset = options.offset ?? 0;
797
- const limit = options.limit ?? lines.length;
798
- if (offset >= lines.length) {
864
+ const limit = options.limit ?? display.lines.length;
865
+ if (offset >= display.lines.length) {
799
866
  return "Offset exceeds output length.";
800
867
  }
801
- const slice = lines.slice(offset, offset + limit);
802
- return toLineNumbered(slice, offset);
868
+ const outputLines = [];
869
+ let bytes = 0;
870
+ let truncatedByBytes = false;
871
+ for (let index = offset; index < Math.min(display.lines.length, offset + limit); index += 1) {
872
+ const line = display.lines[index] ?? "";
873
+ const numberedLine = `${formatLineNumber(index + 1)} ${line}`;
874
+ const size = Buffer.byteLength(numberedLine, "utf8") + (outputLines.length > 0 ? 1 : 0);
875
+ if (bytes + size > MAX_READ_BYTES) {
876
+ truncatedByBytes = true;
877
+ break;
878
+ }
879
+ outputLines.push(line);
880
+ bytes += size;
881
+ }
882
+ const hasMoreLines = display.lines.length > offset + outputLines.length;
883
+ const body = toLineNumbered(outputLines, offset);
884
+ return appendReadSuffix(body, {
885
+ truncatedByBytes,
886
+ hasMoreLines,
887
+ lastReadLine: offset + outputLines.length,
888
+ wrapped: display.wrapped,
889
+ clipped: display.clipped
890
+ });
803
891
  }
804
892
  async grep(refId, options) {
805
893
  const filePath = this.resolvePath(refId);
@@ -810,20 +898,47 @@ var ToolOutputCacheStoreImpl = class {
810
898
  const after = Math.max(0, options.after ?? 0);
811
899
  const maxMatches = Math.max(1, options.max_matches ?? DEFAULT_MAX_MATCHES);
812
900
  const results = [];
901
+ let totalBytes = 0;
902
+ let hasWrappedSnippet = false;
903
+ let hasMoreMatches = false;
904
+ let hasMatch = false;
813
905
  for (let i = 0; i < lines.length; i += 1) {
814
- if (results.length >= maxMatches) break;
906
+ if (results.length >= maxMatches) {
907
+ hasMoreMatches = true;
908
+ break;
909
+ }
815
910
  if (!regex.test(lines[i])) continue;
911
+ hasMatch = true;
816
912
  const start = Math.max(0, i - before);
817
913
  const end = Math.min(lines.length, i + after + 1);
818
- const snippet = toLineNumbered(lines.slice(start, end), start);
819
- results.push(`ref:${normalizeRefId(refId)}
820
- ${snippet}`);
914
+ const snippet = renderPhysicalSnippet(lines, start, end);
915
+ if (snippet.wrapped) hasWrappedSnippet = true;
916
+ const rendered = `ref:${normalizeRefId(refId)}
917
+ ${snippet.text}`;
918
+ const size = Buffer.byteLength(rendered, "utf8") + (results.length > 0 ? Buffer.byteLength("\n\n", "utf8") : 0);
919
+ if (totalBytes + size > MAX_GREP_BYTES) {
920
+ hasMoreMatches = true;
921
+ break;
922
+ }
923
+ results.push(rendered);
924
+ totalBytes += size;
821
925
  }
822
926
  if (!results.length) {
927
+ if (hasMatch) {
928
+ return `Matches found but output exceeded ${MAX_GREP_BYTES} bytes. Reduce context (before/after) and retry.`;
929
+ }
823
930
  return `No matches for: ${options.pattern}`;
824
931
  }
825
- return results.length >= maxMatches ? `${results.join("\n\n")}
826
- ... (truncated)` : results.join("\n\n");
932
+ let output = results.join("\n\n");
933
+ if (hasMoreMatches) {
934
+ output += "\n... (truncated)";
935
+ }
936
+ if (hasWrappedSnippet) {
937
+ output += `
938
+
939
+ Long physical lines are wrapped at ${MAX_LINE_LENGTH} chars per display line.`;
940
+ }
941
+ return output;
827
942
  }
828
943
  resolvePath(refId) {
829
944
  return import_node_path5.default.join(this.baseDir, `${normalizeRefId(refId)}.txt`);
package/dist/index.js CHANGED
@@ -727,9 +727,74 @@ var SessionStateStoreImpl = class {
727
727
  import { promises as fs5 } from "fs";
728
728
  import path5 from "path";
729
729
  var DEFAULT_MAX_MATCHES = 50;
730
+ var MAX_LINE_LENGTH = 2e3;
731
+ var MAX_READ_BYTES = 50 * 1024;
732
+ var MAX_GREP_BYTES = 50 * 1024;
730
733
  var normalizeRefId = (refId) => refId.replace(/[^a-zA-Z0-9_-]/g, "_");
731
734
  var formatLineNumber = (lineNumber) => String(lineNumber).padStart(5, " ");
732
735
  var toLineNumbered = (lines, offset) => lines.map((line, index) => `${formatLineNumber(index + offset + 1)} ${line}`).join("\n");
736
+ var splitLongLine = (line) => {
737
+ if (line.length <= MAX_LINE_LENGTH) {
738
+ return [line];
739
+ }
740
+ const chunks = [];
741
+ for (let start = 0; start < line.length; start += MAX_LINE_LENGTH) {
742
+ chunks.push(line.slice(start, start + MAX_LINE_LENGTH));
743
+ }
744
+ return chunks;
745
+ };
746
+ var toDisplayLines = (physicalLines, options) => {
747
+ let wrapped = false;
748
+ let clipped = false;
749
+ const display = [];
750
+ for (const line of physicalLines) {
751
+ if (options.wrapLongLines) {
752
+ const chunks = splitLongLine(line);
753
+ if (chunks.length > 1) wrapped = true;
754
+ display.push(...chunks);
755
+ continue;
756
+ }
757
+ if (line.length > MAX_LINE_LENGTH) {
758
+ clipped = true;
759
+ display.push(`${line.slice(0, MAX_LINE_LENGTH)}...`);
760
+ continue;
761
+ }
762
+ display.push(line);
763
+ }
764
+ return { lines: display, wrapped, clipped };
765
+ };
766
+ var appendReadSuffix = (output, options) => {
767
+ let next = output;
768
+ if (options.truncatedByBytes || options.hasMoreLines) {
769
+ const reason = options.truncatedByBytes ? `Output truncated at ${MAX_READ_BYTES} bytes.` : "Output has more lines.";
770
+ next += `
771
+
772
+ ${reason} Use offset to read beyond line ${options.lastReadLine}.`;
773
+ }
774
+ if (options.wrapped) {
775
+ next += `
776
+
777
+ Long physical lines are wrapped at ${MAX_LINE_LENGTH} chars per display line. Wrapped chunks are display-only and may not match exact source text for edit.`;
778
+ }
779
+ if (options.clipped) {
780
+ next += `
781
+
782
+ Long physical lines are clipped at ${MAX_LINE_LENGTH} chars. Set wrap_long_lines=true to paginate full lines. Clipped output is display-only and may not match exact source text for edit.`;
783
+ }
784
+ return next;
785
+ };
786
+ var renderPhysicalSnippet = (physicalLines, start, end) => {
787
+ let wrapped = false;
788
+ const output = [];
789
+ for (let index = start; index < end; index += 1) {
790
+ const chunks = splitLongLine(physicalLines[index] ?? "");
791
+ if (chunks.length > 1) wrapped = true;
792
+ for (const chunk of chunks) {
793
+ output.push(`${formatLineNumber(index + 1)} ${chunk}`);
794
+ }
795
+ }
796
+ return { text: output.join("\n"), wrapped };
797
+ };
733
798
  var ToolOutputCacheStoreImpl = class {
734
799
  baseDir;
735
800
  constructor(options = {}) {
@@ -748,14 +813,37 @@ var ToolOutputCacheStoreImpl = class {
748
813
  async read(refId, options = {}) {
749
814
  const filePath = this.resolvePath(refId);
750
815
  const content = await fs5.readFile(filePath, "utf8");
751
- const lines = content.split(/\r?\n/);
816
+ const physicalLines = content.split(/\r?\n/);
817
+ const wrapLongLines = options.wrap_long_lines ?? false;
818
+ const display = toDisplayLines(physicalLines, { wrapLongLines });
752
819
  const offset = options.offset ?? 0;
753
- const limit = options.limit ?? lines.length;
754
- if (offset >= lines.length) {
820
+ const limit = options.limit ?? display.lines.length;
821
+ if (offset >= display.lines.length) {
755
822
  return "Offset exceeds output length.";
756
823
  }
757
- const slice = lines.slice(offset, offset + limit);
758
- return toLineNumbered(slice, offset);
824
+ const outputLines = [];
825
+ let bytes = 0;
826
+ let truncatedByBytes = false;
827
+ for (let index = offset; index < Math.min(display.lines.length, offset + limit); index += 1) {
828
+ const line = display.lines[index] ?? "";
829
+ const numberedLine = `${formatLineNumber(index + 1)} ${line}`;
830
+ const size = Buffer.byteLength(numberedLine, "utf8") + (outputLines.length > 0 ? 1 : 0);
831
+ if (bytes + size > MAX_READ_BYTES) {
832
+ truncatedByBytes = true;
833
+ break;
834
+ }
835
+ outputLines.push(line);
836
+ bytes += size;
837
+ }
838
+ const hasMoreLines = display.lines.length > offset + outputLines.length;
839
+ const body = toLineNumbered(outputLines, offset);
840
+ return appendReadSuffix(body, {
841
+ truncatedByBytes,
842
+ hasMoreLines,
843
+ lastReadLine: offset + outputLines.length,
844
+ wrapped: display.wrapped,
845
+ clipped: display.clipped
846
+ });
759
847
  }
760
848
  async grep(refId, options) {
761
849
  const filePath = this.resolvePath(refId);
@@ -766,20 +854,47 @@ var ToolOutputCacheStoreImpl = class {
766
854
  const after = Math.max(0, options.after ?? 0);
767
855
  const maxMatches = Math.max(1, options.max_matches ?? DEFAULT_MAX_MATCHES);
768
856
  const results = [];
857
+ let totalBytes = 0;
858
+ let hasWrappedSnippet = false;
859
+ let hasMoreMatches = false;
860
+ let hasMatch = false;
769
861
  for (let i = 0; i < lines.length; i += 1) {
770
- if (results.length >= maxMatches) break;
862
+ if (results.length >= maxMatches) {
863
+ hasMoreMatches = true;
864
+ break;
865
+ }
771
866
  if (!regex.test(lines[i])) continue;
867
+ hasMatch = true;
772
868
  const start = Math.max(0, i - before);
773
869
  const end = Math.min(lines.length, i + after + 1);
774
- const snippet = toLineNumbered(lines.slice(start, end), start);
775
- results.push(`ref:${normalizeRefId(refId)}
776
- ${snippet}`);
870
+ const snippet = renderPhysicalSnippet(lines, start, end);
871
+ if (snippet.wrapped) hasWrappedSnippet = true;
872
+ const rendered = `ref:${normalizeRefId(refId)}
873
+ ${snippet.text}`;
874
+ const size = Buffer.byteLength(rendered, "utf8") + (results.length > 0 ? Buffer.byteLength("\n\n", "utf8") : 0);
875
+ if (totalBytes + size > MAX_GREP_BYTES) {
876
+ hasMoreMatches = true;
877
+ break;
878
+ }
879
+ results.push(rendered);
880
+ totalBytes += size;
777
881
  }
778
882
  if (!results.length) {
883
+ if (hasMatch) {
884
+ return `Matches found but output exceeded ${MAX_GREP_BYTES} bytes. Reduce context (before/after) and retry.`;
885
+ }
779
886
  return `No matches for: ${options.pattern}`;
780
887
  }
781
- return results.length >= maxMatches ? `${results.join("\n\n")}
782
- ... (truncated)` : results.join("\n\n");
888
+ let output = results.join("\n\n");
889
+ if (hasMoreMatches) {
890
+ output += "\n... (truncated)";
891
+ }
892
+ if (hasWrappedSnippet) {
893
+ output += `
894
+
895
+ Long physical lines are wrapped at ${MAX_LINE_LENGTH} chars per display line.`;
896
+ }
897
+ return output;
783
898
  }
784
899
  resolvePath(refId) {
785
900
  return path5.join(this.baseDir, `${normalizeRefId(refId)}.txt`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codelia/storage",
3
- "version": "0.1.35",
3
+ "version": "0.1.36",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=20 <25"
@@ -23,8 +23,8 @@
23
23
  "typecheck": "tsc --noEmit"
24
24
  },
25
25
  "dependencies": {
26
- "@codelia/core": "0.1.35",
27
- "@codelia/shared-types": "0.1.35",
26
+ "@codelia/core": "0.1.36",
27
+ "@codelia/shared-types": "0.1.36",
28
28
  "better-sqlite3": "^12.6.2"
29
29
  },
30
30
  "publishConfig": {