@codelia/storage 0.1.36 → 0.1.37
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/index.cjs +191 -66
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +191 -66
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -771,79 +771,114 @@ 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
|
|
775
|
-
var
|
|
776
|
-
var
|
|
774
|
+
var DEFAULT_MAX_LINE_LENGTH = 5e4;
|
|
775
|
+
var DEFAULT_MAX_READ_BYTES = 64 * 1024;
|
|
776
|
+
var DEFAULT_MAX_GREP_BYTES = 64 * 1024;
|
|
777
|
+
var DEFAULT_READ_LINE_CHAR_LIMIT = 1e4;
|
|
778
|
+
var MAX_READ_LINE_CHAR_LIMIT = 1e5;
|
|
777
779
|
var normalizeRefId = (refId) => refId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
778
780
|
var formatLineNumber = (lineNumber) => String(lineNumber).padStart(5, " ");
|
|
779
781
|
var toLineNumbered = (lines, offset) => lines.map((line, index) => `${formatLineNumber(index + offset + 1)} ${line}`).join("\n");
|
|
780
|
-
var
|
|
781
|
-
if (
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
782
|
+
var normalizeLimit = (value, fallback) => {
|
|
783
|
+
if (value === void 0) return fallback;
|
|
784
|
+
if (!Number.isFinite(value) || value <= 0) return fallback;
|
|
785
|
+
return Math.trunc(value);
|
|
786
|
+
};
|
|
787
|
+
var parsePositiveIntEnv = (key) => {
|
|
788
|
+
const raw = process.env[key];
|
|
789
|
+
if (!raw) return void 0;
|
|
790
|
+
const parsed = Number.parseInt(raw, 10);
|
|
791
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return void 0;
|
|
792
|
+
return parsed;
|
|
793
|
+
};
|
|
794
|
+
var buildTooLargeToReadError = (options) => [
|
|
795
|
+
`TOO_LARGE_TO_READ: output exceeds per-call limit (${options.maxReadBytes} bytes).`,
|
|
796
|
+
`ref_id=${options.refId} offset=${options.offset} limit=${options.limit}`,
|
|
797
|
+
"Try split reads:",
|
|
798
|
+
`- tool_output_cache({"ref_id":"${options.refId}","offset":${options.offset},"limit":2000})`,
|
|
799
|
+
`- tool_output_cache({"ref_id":"${options.refId}","offset":${options.nextOffset},"limit":2000})`
|
|
800
|
+
].join("\n").trim();
|
|
801
|
+
var buildSingleLineByteCapError = (options) => [
|
|
802
|
+
`TOO_LARGE_TO_READ: requested line ${options.lineNumber} exceeds per-call byte limit (${options.maxReadBytes} bytes).`,
|
|
803
|
+
`ref_id=${options.refId} line_number=${options.lineNumber}`,
|
|
804
|
+
"Offset-based pagination cannot continue within one physical line.",
|
|
805
|
+
"Use tool_output_cache_line for char-based paging or retry with allow_truncate=true.",
|
|
806
|
+
`tool_output_cache_line({"ref_id":"${options.refId}","line_number":${options.lineNumber},"char_offset":0,"char_limit":10000})`
|
|
807
|
+
].join("\n").trim();
|
|
808
|
+
var buildLineTooLongError = (options) => [
|
|
809
|
+
`LINE_TOO_LONG: line ${options.lineNumber} has ${options.lineLength} chars (max ${options.maxLineLength}).`,
|
|
810
|
+
`ref_id=${options.refId}`,
|
|
811
|
+
"Use tool_output_cache_line for char-based paging or retry with allow_truncate=true.",
|
|
812
|
+
`tool_output_cache_line({"ref_id":"${options.refId}","line_number":${options.lineNumber},"char_offset":0,"char_limit":10000})`
|
|
813
|
+
].join("\n").trim();
|
|
814
|
+
var clipLongLine = (line, maxLineLength) => {
|
|
815
|
+
if (line.length <= maxLineLength) return line;
|
|
816
|
+
return `${line.slice(0, maxLineLength)}...`;
|
|
817
|
+
};
|
|
818
|
+
var clipUtf8ToBytes = (value, maxBytes) => {
|
|
819
|
+
if (maxBytes <= 0 || value.length === 0) return "";
|
|
820
|
+
let bytes = 0;
|
|
821
|
+
let out = "";
|
|
822
|
+
for (const ch of value) {
|
|
823
|
+
const next = Buffer.byteLength(ch, "utf8");
|
|
824
|
+
if (bytes + next > maxBytes) break;
|
|
825
|
+
out += ch;
|
|
826
|
+
bytes += next;
|
|
787
827
|
}
|
|
788
|
-
return
|
|
828
|
+
return out;
|
|
789
829
|
};
|
|
790
|
-
var
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
const
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
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 };
|
|
830
|
+
var clipLineToByteBudget = (line, maxBytes) => {
|
|
831
|
+
if (maxBytes <= 0) return "";
|
|
832
|
+
if (Buffer.byteLength(line, "utf8") <= maxBytes) return line;
|
|
833
|
+
const suffix = "...";
|
|
834
|
+
const suffixBytes = Buffer.byteLength(suffix, "utf8");
|
|
835
|
+
if (maxBytes <= suffixBytes) {
|
|
836
|
+
return clipUtf8ToBytes(line, maxBytes);
|
|
837
|
+
}
|
|
838
|
+
return `${clipUtf8ToBytes(line, maxBytes - suffixBytes)}${suffix}`;
|
|
809
839
|
};
|
|
810
840
|
var appendReadSuffix = (output, options) => {
|
|
811
841
|
let next = output;
|
|
812
842
|
if (options.truncatedByBytes || options.hasMoreLines) {
|
|
813
|
-
const reason = options.truncatedByBytes ? `Output truncated at ${
|
|
843
|
+
const reason = options.truncatedByBytes ? `Output truncated at ${options.maxReadBytes} bytes.` : "Output has more lines.";
|
|
814
844
|
next += `
|
|
815
845
|
|
|
816
846
|
${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
847
|
}
|
|
823
848
|
if (options.clipped) {
|
|
824
849
|
next += `
|
|
825
850
|
|
|
826
|
-
Long physical lines are clipped at ${
|
|
851
|
+
Long physical lines are clipped at ${options.maxLineLength} chars.`;
|
|
827
852
|
}
|
|
828
853
|
return next;
|
|
829
854
|
};
|
|
830
855
|
var renderPhysicalSnippet = (physicalLines, start, end) => {
|
|
831
|
-
let wrapped = false;
|
|
832
856
|
const output = [];
|
|
833
857
|
for (let index = start; index < end; index += 1) {
|
|
834
|
-
|
|
835
|
-
if (chunks.length > 1) wrapped = true;
|
|
836
|
-
for (const chunk of chunks) {
|
|
837
|
-
output.push(`${formatLineNumber(index + 1)} ${chunk}`);
|
|
838
|
-
}
|
|
858
|
+
output.push(`${formatLineNumber(index + 1)} ${physicalLines[index] ?? ""}`);
|
|
839
859
|
}
|
|
840
|
-
return
|
|
860
|
+
return output.join("\n");
|
|
841
861
|
};
|
|
842
862
|
var ToolOutputCacheStoreImpl = class {
|
|
843
863
|
baseDir;
|
|
864
|
+
maxLineLength;
|
|
865
|
+
maxReadBytes;
|
|
866
|
+
maxGrepBytes;
|
|
844
867
|
constructor(options = {}) {
|
|
845
868
|
const paths = options.paths ?? resolveStoragePaths();
|
|
846
869
|
this.baseDir = paths.toolOutputCacheDir;
|
|
870
|
+
this.maxLineLength = normalizeLimit(
|
|
871
|
+
options.limits?.maxLineLength ?? parsePositiveIntEnv("CODELIA_TOOL_OUTPUT_CACHE_MAX_LINE_LENGTH"),
|
|
872
|
+
DEFAULT_MAX_LINE_LENGTH
|
|
873
|
+
);
|
|
874
|
+
this.maxReadBytes = normalizeLimit(
|
|
875
|
+
options.limits?.maxReadBytes ?? parsePositiveIntEnv("CODELIA_TOOL_OUTPUT_CACHE_MAX_READ_BYTES"),
|
|
876
|
+
DEFAULT_MAX_READ_BYTES
|
|
877
|
+
);
|
|
878
|
+
this.maxGrepBytes = normalizeLimit(
|
|
879
|
+
options.limits?.maxGrepBytes ?? parsePositiveIntEnv("CODELIA_TOOL_OUTPUT_CACHE_MAX_GREP_BYTES"),
|
|
880
|
+
DEFAULT_MAX_GREP_BYTES
|
|
881
|
+
);
|
|
847
882
|
}
|
|
848
883
|
async save(record) {
|
|
849
884
|
const refId = normalizeRefId(record.tool_call_id);
|
|
@@ -857,37 +892,126 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
857
892
|
async read(refId, options = {}) {
|
|
858
893
|
const filePath = this.resolvePath(refId);
|
|
859
894
|
const content = await import_node_fs5.promises.readFile(filePath, "utf8");
|
|
860
|
-
const
|
|
861
|
-
const wrapLongLines = options.wrap_long_lines ?? false;
|
|
862
|
-
const display = toDisplayLines(physicalLines, { wrapLongLines });
|
|
895
|
+
const lines = content.split(/\r?\n/);
|
|
863
896
|
const offset = options.offset ?? 0;
|
|
864
|
-
const limit = options.limit ??
|
|
865
|
-
|
|
897
|
+
const limit = options.limit ?? lines.length;
|
|
898
|
+
const allowTruncate = options.allow_truncate ?? false;
|
|
899
|
+
if (offset >= lines.length) {
|
|
866
900
|
return "Offset exceeds output length.";
|
|
867
901
|
}
|
|
868
902
|
const outputLines = [];
|
|
869
903
|
let bytes = 0;
|
|
870
904
|
let truncatedByBytes = false;
|
|
871
|
-
|
|
872
|
-
|
|
905
|
+
let clipped = false;
|
|
906
|
+
let firstClippedLineNumber = null;
|
|
907
|
+
for (let index = offset; index < Math.min(lines.length, offset + limit); index += 1) {
|
|
908
|
+
const originalLine = lines[index] ?? "";
|
|
909
|
+
if (originalLine.length > this.maxLineLength && !allowTruncate) {
|
|
910
|
+
return buildLineTooLongError({
|
|
911
|
+
refId: normalizeRefId(refId),
|
|
912
|
+
lineNumber: index + 1,
|
|
913
|
+
lineLength: originalLine.length,
|
|
914
|
+
maxLineLength: this.maxLineLength
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
const line = allowTruncate ? clipLongLine(originalLine, this.maxLineLength) : originalLine;
|
|
918
|
+
if (line.length !== originalLine.length) {
|
|
919
|
+
clipped = true;
|
|
920
|
+
if (firstClippedLineNumber === null) {
|
|
921
|
+
firstClippedLineNumber = index + 1;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
873
924
|
const numberedLine = `${formatLineNumber(index + 1)} ${line}`;
|
|
874
925
|
const size = Buffer.byteLength(numberedLine, "utf8") + (outputLines.length > 0 ? 1 : 0);
|
|
875
|
-
if (bytes + size >
|
|
926
|
+
if (bytes + size > this.maxReadBytes) {
|
|
927
|
+
if (!allowTruncate) {
|
|
928
|
+
if (outputLines.length === 0) {
|
|
929
|
+
return buildSingleLineByteCapError({
|
|
930
|
+
refId: normalizeRefId(refId),
|
|
931
|
+
lineNumber: index + 1,
|
|
932
|
+
maxReadBytes: this.maxReadBytes
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
return buildTooLargeToReadError({
|
|
936
|
+
refId: normalizeRefId(refId),
|
|
937
|
+
offset,
|
|
938
|
+
limit,
|
|
939
|
+
maxReadBytes: this.maxReadBytes,
|
|
940
|
+
nextOffset: Math.max(offset + outputLines.length, offset + 1)
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
if (outputLines.length === 0) {
|
|
944
|
+
const prefix = `${formatLineNumber(index + 1)} `;
|
|
945
|
+
const budget = this.maxReadBytes - Buffer.byteLength(prefix, "utf8");
|
|
946
|
+
const clippedByBytes = clipLineToByteBudget(line, budget);
|
|
947
|
+
if (clippedByBytes.length > 0) {
|
|
948
|
+
outputLines.push(clippedByBytes);
|
|
949
|
+
bytes = Buffer.byteLength(`${prefix}${clippedByBytes}`, "utf8");
|
|
950
|
+
clipped = true;
|
|
951
|
+
if (firstClippedLineNumber === null) {
|
|
952
|
+
firstClippedLineNumber = index + 1;
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
876
956
|
truncatedByBytes = true;
|
|
877
957
|
break;
|
|
878
958
|
}
|
|
879
959
|
outputLines.push(line);
|
|
880
960
|
bytes += size;
|
|
881
961
|
}
|
|
882
|
-
const hasMoreLines =
|
|
962
|
+
const hasMoreLines = lines.length > offset + outputLines.length;
|
|
883
963
|
const body = toLineNumbered(outputLines, offset);
|
|
884
964
|
return appendReadSuffix(body, {
|
|
885
965
|
truncatedByBytes,
|
|
886
966
|
hasMoreLines,
|
|
887
967
|
lastReadLine: offset + outputLines.length,
|
|
888
|
-
|
|
889
|
-
clipped
|
|
890
|
-
|
|
968
|
+
maxReadBytes: this.maxReadBytes,
|
|
969
|
+
clipped,
|
|
970
|
+
maxLineLength: this.maxLineLength
|
|
971
|
+
}).concat(
|
|
972
|
+
allowTruncate && firstClippedLineNumber !== null ? `
|
|
973
|
+
|
|
974
|
+
For full long-line content, use tool_output_cache_line({"ref_id":"${normalizeRefId(refId)}","line_number":${firstClippedLineNumber},"char_offset":0,"char_limit":10000}).` : ""
|
|
975
|
+
);
|
|
976
|
+
}
|
|
977
|
+
async readLine(refId, options) {
|
|
978
|
+
const filePath = this.resolvePath(refId);
|
|
979
|
+
const content = await import_node_fs5.promises.readFile(filePath, "utf8");
|
|
980
|
+
const lines = content.split(/\r?\n/);
|
|
981
|
+
const lineIndex = options.line_number - 1;
|
|
982
|
+
if (lineIndex < 0 || lineIndex >= lines.length) {
|
|
983
|
+
return `Line number out of range: ${options.line_number} (total ${lines.length})`;
|
|
984
|
+
}
|
|
985
|
+
const line = lines[lineIndex] ?? "";
|
|
986
|
+
const charOffset = Math.max(0, options.char_offset ?? 0);
|
|
987
|
+
const charLimit = Math.max(
|
|
988
|
+
1,
|
|
989
|
+
Math.min(
|
|
990
|
+
Math.trunc(options.char_limit ?? DEFAULT_READ_LINE_CHAR_LIMIT),
|
|
991
|
+
MAX_READ_LINE_CHAR_LIMIT
|
|
992
|
+
)
|
|
993
|
+
);
|
|
994
|
+
if (charOffset > line.length) {
|
|
995
|
+
return `char_offset out of range: ${charOffset} (line length ${line.length})`;
|
|
996
|
+
}
|
|
997
|
+
const segment = line.slice(charOffset, charOffset + charLimit);
|
|
998
|
+
const endOffset = charOffset + segment.length;
|
|
999
|
+
const hasMore = endOffset < line.length;
|
|
1000
|
+
let output = [
|
|
1001
|
+
`ref_id=${normalizeRefId(refId)}`,
|
|
1002
|
+
`line_number=${options.line_number}`,
|
|
1003
|
+
`line_length=${line.length}`,
|
|
1004
|
+
`char_range=${charOffset}..${Math.max(endOffset - 1, charOffset - 1)}`,
|
|
1005
|
+
segment
|
|
1006
|
+
].join("\n");
|
|
1007
|
+
if (hasMore) {
|
|
1008
|
+
output += `
|
|
1009
|
+
|
|
1010
|
+
Use char_offset=${endOffset} to continue.`;
|
|
1011
|
+
output += `
|
|
1012
|
+
tool_output_cache_line({"ref_id":"${normalizeRefId(refId)}","line_number":${options.line_number},"char_offset":${endOffset},"char_limit":${charLimit}})`;
|
|
1013
|
+
}
|
|
1014
|
+
return output;
|
|
891
1015
|
}
|
|
892
1016
|
async grep(refId, options) {
|
|
893
1017
|
const filePath = this.resolvePath(refId);
|
|
@@ -899,7 +1023,6 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
899
1023
|
const maxMatches = Math.max(1, options.max_matches ?? DEFAULT_MAX_MATCHES);
|
|
900
1024
|
const results = [];
|
|
901
1025
|
let totalBytes = 0;
|
|
902
|
-
let hasWrappedSnippet = false;
|
|
903
1026
|
let hasMoreMatches = false;
|
|
904
1027
|
let hasMatch = false;
|
|
905
1028
|
for (let i = 0; i < lines.length; i += 1) {
|
|
@@ -912,11 +1035,18 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
912
1035
|
const start = Math.max(0, i - before);
|
|
913
1036
|
const end = Math.min(lines.length, i + after + 1);
|
|
914
1037
|
const snippet = renderPhysicalSnippet(lines, start, end);
|
|
915
|
-
if (snippet.wrapped) hasWrappedSnippet = true;
|
|
916
1038
|
const rendered = `ref:${normalizeRefId(refId)}
|
|
917
|
-
${snippet
|
|
1039
|
+
${snippet}`;
|
|
918
1040
|
const size = Buffer.byteLength(rendered, "utf8") + (results.length > 0 ? Buffer.byteLength("\n\n", "utf8") : 0);
|
|
919
|
-
if (totalBytes + size >
|
|
1041
|
+
if (totalBytes + size > this.maxGrepBytes) {
|
|
1042
|
+
if (results.length === 0) {
|
|
1043
|
+
return [
|
|
1044
|
+
`MATCH_TOO_LARGE_TO_RENDER: match at line ${i + 1} exceeded ${this.maxGrepBytes} bytes.`,
|
|
1045
|
+
`ref_id=${normalizeRefId(refId)}`,
|
|
1046
|
+
"Reduce grep context (before/after) or read the matched line directly:",
|
|
1047
|
+
`tool_output_cache_line({"ref_id":"${normalizeRefId(refId)}","line_number":${i + 1},"char_offset":0,"char_limit":10000})`
|
|
1048
|
+
].join("\n");
|
|
1049
|
+
}
|
|
920
1050
|
hasMoreMatches = true;
|
|
921
1051
|
break;
|
|
922
1052
|
}
|
|
@@ -925,7 +1055,7 @@ ${snippet.text}`;
|
|
|
925
1055
|
}
|
|
926
1056
|
if (!results.length) {
|
|
927
1057
|
if (hasMatch) {
|
|
928
|
-
return `Matches found but output exceeded ${
|
|
1058
|
+
return `Matches found but output exceeded ${this.maxGrepBytes} bytes. Reduce context (before/after) and retry.`;
|
|
929
1059
|
}
|
|
930
1060
|
return `No matches for: ${options.pattern}`;
|
|
931
1061
|
}
|
|
@@ -933,11 +1063,6 @@ ${snippet.text}`;
|
|
|
933
1063
|
if (hasMoreMatches) {
|
|
934
1064
|
output += "\n... (truncated)";
|
|
935
1065
|
}
|
|
936
|
-
if (hasWrappedSnippet) {
|
|
937
|
-
output += `
|
|
938
|
-
|
|
939
|
-
Long physical lines are wrapped at ${MAX_LINE_LENGTH} chars per display line.`;
|
|
940
|
-
}
|
|
941
1066
|
return output;
|
|
942
1067
|
}
|
|
943
1068
|
resolvePath(refId) {
|
package/dist/index.d.cts
CHANGED
|
@@ -95,13 +95,27 @@ declare class SessionStoreWriterImpl implements SessionStore {
|
|
|
95
95
|
append(record: SessionRecord): Promise<void>;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
type ToolOutputCacheLimits = {
|
|
99
|
+
maxLineLength?: number;
|
|
100
|
+
maxReadBytes?: number;
|
|
101
|
+
maxGrepBytes?: number;
|
|
102
|
+
};
|
|
98
103
|
declare class ToolOutputCacheStoreImpl implements ToolOutputCacheStore {
|
|
99
104
|
private readonly baseDir;
|
|
105
|
+
private readonly maxLineLength;
|
|
106
|
+
private readonly maxReadBytes;
|
|
107
|
+
private readonly maxGrepBytes;
|
|
100
108
|
constructor(options?: {
|
|
101
109
|
paths?: StoragePaths;
|
|
110
|
+
limits?: ToolOutputCacheLimits;
|
|
102
111
|
});
|
|
103
112
|
save(record: ToolOutputCacheRecord): Promise<ToolOutputRef>;
|
|
104
113
|
read(refId: string, options?: ToolOutputCacheReadOptions): Promise<string>;
|
|
114
|
+
readLine(refId: string, options: {
|
|
115
|
+
line_number: number;
|
|
116
|
+
char_offset?: number;
|
|
117
|
+
char_limit?: number;
|
|
118
|
+
}): Promise<string>;
|
|
105
119
|
grep(refId: string, options: ToolOutputCacheSearchOptions): Promise<string>;
|
|
106
120
|
private resolvePath;
|
|
107
121
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -95,13 +95,27 @@ declare class SessionStoreWriterImpl implements SessionStore {
|
|
|
95
95
|
append(record: SessionRecord): Promise<void>;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
type ToolOutputCacheLimits = {
|
|
99
|
+
maxLineLength?: number;
|
|
100
|
+
maxReadBytes?: number;
|
|
101
|
+
maxGrepBytes?: number;
|
|
102
|
+
};
|
|
98
103
|
declare class ToolOutputCacheStoreImpl implements ToolOutputCacheStore {
|
|
99
104
|
private readonly baseDir;
|
|
105
|
+
private readonly maxLineLength;
|
|
106
|
+
private readonly maxReadBytes;
|
|
107
|
+
private readonly maxGrepBytes;
|
|
100
108
|
constructor(options?: {
|
|
101
109
|
paths?: StoragePaths;
|
|
110
|
+
limits?: ToolOutputCacheLimits;
|
|
102
111
|
});
|
|
103
112
|
save(record: ToolOutputCacheRecord): Promise<ToolOutputRef>;
|
|
104
113
|
read(refId: string, options?: ToolOutputCacheReadOptions): Promise<string>;
|
|
114
|
+
readLine(refId: string, options: {
|
|
115
|
+
line_number: number;
|
|
116
|
+
char_offset?: number;
|
|
117
|
+
char_limit?: number;
|
|
118
|
+
}): Promise<string>;
|
|
105
119
|
grep(refId: string, options: ToolOutputCacheSearchOptions): Promise<string>;
|
|
106
120
|
private resolvePath;
|
|
107
121
|
}
|
package/dist/index.js
CHANGED
|
@@ -727,79 +727,114 @@ 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
|
|
731
|
-
var
|
|
732
|
-
var
|
|
730
|
+
var DEFAULT_MAX_LINE_LENGTH = 5e4;
|
|
731
|
+
var DEFAULT_MAX_READ_BYTES = 64 * 1024;
|
|
732
|
+
var DEFAULT_MAX_GREP_BYTES = 64 * 1024;
|
|
733
|
+
var DEFAULT_READ_LINE_CHAR_LIMIT = 1e4;
|
|
734
|
+
var MAX_READ_LINE_CHAR_LIMIT = 1e5;
|
|
733
735
|
var normalizeRefId = (refId) => refId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
734
736
|
var formatLineNumber = (lineNumber) => String(lineNumber).padStart(5, " ");
|
|
735
737
|
var toLineNumbered = (lines, offset) => lines.map((line, index) => `${formatLineNumber(index + offset + 1)} ${line}`).join("\n");
|
|
736
|
-
var
|
|
737
|
-
if (
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
738
|
+
var normalizeLimit = (value, fallback) => {
|
|
739
|
+
if (value === void 0) return fallback;
|
|
740
|
+
if (!Number.isFinite(value) || value <= 0) return fallback;
|
|
741
|
+
return Math.trunc(value);
|
|
742
|
+
};
|
|
743
|
+
var parsePositiveIntEnv = (key) => {
|
|
744
|
+
const raw = process.env[key];
|
|
745
|
+
if (!raw) return void 0;
|
|
746
|
+
const parsed = Number.parseInt(raw, 10);
|
|
747
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return void 0;
|
|
748
|
+
return parsed;
|
|
749
|
+
};
|
|
750
|
+
var buildTooLargeToReadError = (options) => [
|
|
751
|
+
`TOO_LARGE_TO_READ: output exceeds per-call limit (${options.maxReadBytes} bytes).`,
|
|
752
|
+
`ref_id=${options.refId} offset=${options.offset} limit=${options.limit}`,
|
|
753
|
+
"Try split reads:",
|
|
754
|
+
`- tool_output_cache({"ref_id":"${options.refId}","offset":${options.offset},"limit":2000})`,
|
|
755
|
+
`- tool_output_cache({"ref_id":"${options.refId}","offset":${options.nextOffset},"limit":2000})`
|
|
756
|
+
].join("\n").trim();
|
|
757
|
+
var buildSingleLineByteCapError = (options) => [
|
|
758
|
+
`TOO_LARGE_TO_READ: requested line ${options.lineNumber} exceeds per-call byte limit (${options.maxReadBytes} bytes).`,
|
|
759
|
+
`ref_id=${options.refId} line_number=${options.lineNumber}`,
|
|
760
|
+
"Offset-based pagination cannot continue within one physical line.",
|
|
761
|
+
"Use tool_output_cache_line for char-based paging or retry with allow_truncate=true.",
|
|
762
|
+
`tool_output_cache_line({"ref_id":"${options.refId}","line_number":${options.lineNumber},"char_offset":0,"char_limit":10000})`
|
|
763
|
+
].join("\n").trim();
|
|
764
|
+
var buildLineTooLongError = (options) => [
|
|
765
|
+
`LINE_TOO_LONG: line ${options.lineNumber} has ${options.lineLength} chars (max ${options.maxLineLength}).`,
|
|
766
|
+
`ref_id=${options.refId}`,
|
|
767
|
+
"Use tool_output_cache_line for char-based paging or retry with allow_truncate=true.",
|
|
768
|
+
`tool_output_cache_line({"ref_id":"${options.refId}","line_number":${options.lineNumber},"char_offset":0,"char_limit":10000})`
|
|
769
|
+
].join("\n").trim();
|
|
770
|
+
var clipLongLine = (line, maxLineLength) => {
|
|
771
|
+
if (line.length <= maxLineLength) return line;
|
|
772
|
+
return `${line.slice(0, maxLineLength)}...`;
|
|
773
|
+
};
|
|
774
|
+
var clipUtf8ToBytes = (value, maxBytes) => {
|
|
775
|
+
if (maxBytes <= 0 || value.length === 0) return "";
|
|
776
|
+
let bytes = 0;
|
|
777
|
+
let out = "";
|
|
778
|
+
for (const ch of value) {
|
|
779
|
+
const next = Buffer.byteLength(ch, "utf8");
|
|
780
|
+
if (bytes + next > maxBytes) break;
|
|
781
|
+
out += ch;
|
|
782
|
+
bytes += next;
|
|
743
783
|
}
|
|
744
|
-
return
|
|
784
|
+
return out;
|
|
745
785
|
};
|
|
746
|
-
var
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
const
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
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 };
|
|
786
|
+
var clipLineToByteBudget = (line, maxBytes) => {
|
|
787
|
+
if (maxBytes <= 0) return "";
|
|
788
|
+
if (Buffer.byteLength(line, "utf8") <= maxBytes) return line;
|
|
789
|
+
const suffix = "...";
|
|
790
|
+
const suffixBytes = Buffer.byteLength(suffix, "utf8");
|
|
791
|
+
if (maxBytes <= suffixBytes) {
|
|
792
|
+
return clipUtf8ToBytes(line, maxBytes);
|
|
793
|
+
}
|
|
794
|
+
return `${clipUtf8ToBytes(line, maxBytes - suffixBytes)}${suffix}`;
|
|
765
795
|
};
|
|
766
796
|
var appendReadSuffix = (output, options) => {
|
|
767
797
|
let next = output;
|
|
768
798
|
if (options.truncatedByBytes || options.hasMoreLines) {
|
|
769
|
-
const reason = options.truncatedByBytes ? `Output truncated at ${
|
|
799
|
+
const reason = options.truncatedByBytes ? `Output truncated at ${options.maxReadBytes} bytes.` : "Output has more lines.";
|
|
770
800
|
next += `
|
|
771
801
|
|
|
772
802
|
${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
803
|
}
|
|
779
804
|
if (options.clipped) {
|
|
780
805
|
next += `
|
|
781
806
|
|
|
782
|
-
Long physical lines are clipped at ${
|
|
807
|
+
Long physical lines are clipped at ${options.maxLineLength} chars.`;
|
|
783
808
|
}
|
|
784
809
|
return next;
|
|
785
810
|
};
|
|
786
811
|
var renderPhysicalSnippet = (physicalLines, start, end) => {
|
|
787
|
-
let wrapped = false;
|
|
788
812
|
const output = [];
|
|
789
813
|
for (let index = start; index < end; index += 1) {
|
|
790
|
-
|
|
791
|
-
if (chunks.length > 1) wrapped = true;
|
|
792
|
-
for (const chunk of chunks) {
|
|
793
|
-
output.push(`${formatLineNumber(index + 1)} ${chunk}`);
|
|
794
|
-
}
|
|
814
|
+
output.push(`${formatLineNumber(index + 1)} ${physicalLines[index] ?? ""}`);
|
|
795
815
|
}
|
|
796
|
-
return
|
|
816
|
+
return output.join("\n");
|
|
797
817
|
};
|
|
798
818
|
var ToolOutputCacheStoreImpl = class {
|
|
799
819
|
baseDir;
|
|
820
|
+
maxLineLength;
|
|
821
|
+
maxReadBytes;
|
|
822
|
+
maxGrepBytes;
|
|
800
823
|
constructor(options = {}) {
|
|
801
824
|
const paths = options.paths ?? resolveStoragePaths();
|
|
802
825
|
this.baseDir = paths.toolOutputCacheDir;
|
|
826
|
+
this.maxLineLength = normalizeLimit(
|
|
827
|
+
options.limits?.maxLineLength ?? parsePositiveIntEnv("CODELIA_TOOL_OUTPUT_CACHE_MAX_LINE_LENGTH"),
|
|
828
|
+
DEFAULT_MAX_LINE_LENGTH
|
|
829
|
+
);
|
|
830
|
+
this.maxReadBytes = normalizeLimit(
|
|
831
|
+
options.limits?.maxReadBytes ?? parsePositiveIntEnv("CODELIA_TOOL_OUTPUT_CACHE_MAX_READ_BYTES"),
|
|
832
|
+
DEFAULT_MAX_READ_BYTES
|
|
833
|
+
);
|
|
834
|
+
this.maxGrepBytes = normalizeLimit(
|
|
835
|
+
options.limits?.maxGrepBytes ?? parsePositiveIntEnv("CODELIA_TOOL_OUTPUT_CACHE_MAX_GREP_BYTES"),
|
|
836
|
+
DEFAULT_MAX_GREP_BYTES
|
|
837
|
+
);
|
|
803
838
|
}
|
|
804
839
|
async save(record) {
|
|
805
840
|
const refId = normalizeRefId(record.tool_call_id);
|
|
@@ -813,37 +848,126 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
813
848
|
async read(refId, options = {}) {
|
|
814
849
|
const filePath = this.resolvePath(refId);
|
|
815
850
|
const content = await fs5.readFile(filePath, "utf8");
|
|
816
|
-
const
|
|
817
|
-
const wrapLongLines = options.wrap_long_lines ?? false;
|
|
818
|
-
const display = toDisplayLines(physicalLines, { wrapLongLines });
|
|
851
|
+
const lines = content.split(/\r?\n/);
|
|
819
852
|
const offset = options.offset ?? 0;
|
|
820
|
-
const limit = options.limit ??
|
|
821
|
-
|
|
853
|
+
const limit = options.limit ?? lines.length;
|
|
854
|
+
const allowTruncate = options.allow_truncate ?? false;
|
|
855
|
+
if (offset >= lines.length) {
|
|
822
856
|
return "Offset exceeds output length.";
|
|
823
857
|
}
|
|
824
858
|
const outputLines = [];
|
|
825
859
|
let bytes = 0;
|
|
826
860
|
let truncatedByBytes = false;
|
|
827
|
-
|
|
828
|
-
|
|
861
|
+
let clipped = false;
|
|
862
|
+
let firstClippedLineNumber = null;
|
|
863
|
+
for (let index = offset; index < Math.min(lines.length, offset + limit); index += 1) {
|
|
864
|
+
const originalLine = lines[index] ?? "";
|
|
865
|
+
if (originalLine.length > this.maxLineLength && !allowTruncate) {
|
|
866
|
+
return buildLineTooLongError({
|
|
867
|
+
refId: normalizeRefId(refId),
|
|
868
|
+
lineNumber: index + 1,
|
|
869
|
+
lineLength: originalLine.length,
|
|
870
|
+
maxLineLength: this.maxLineLength
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
const line = allowTruncate ? clipLongLine(originalLine, this.maxLineLength) : originalLine;
|
|
874
|
+
if (line.length !== originalLine.length) {
|
|
875
|
+
clipped = true;
|
|
876
|
+
if (firstClippedLineNumber === null) {
|
|
877
|
+
firstClippedLineNumber = index + 1;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
829
880
|
const numberedLine = `${formatLineNumber(index + 1)} ${line}`;
|
|
830
881
|
const size = Buffer.byteLength(numberedLine, "utf8") + (outputLines.length > 0 ? 1 : 0);
|
|
831
|
-
if (bytes + size >
|
|
882
|
+
if (bytes + size > this.maxReadBytes) {
|
|
883
|
+
if (!allowTruncate) {
|
|
884
|
+
if (outputLines.length === 0) {
|
|
885
|
+
return buildSingleLineByteCapError({
|
|
886
|
+
refId: normalizeRefId(refId),
|
|
887
|
+
lineNumber: index + 1,
|
|
888
|
+
maxReadBytes: this.maxReadBytes
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
return buildTooLargeToReadError({
|
|
892
|
+
refId: normalizeRefId(refId),
|
|
893
|
+
offset,
|
|
894
|
+
limit,
|
|
895
|
+
maxReadBytes: this.maxReadBytes,
|
|
896
|
+
nextOffset: Math.max(offset + outputLines.length, offset + 1)
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
if (outputLines.length === 0) {
|
|
900
|
+
const prefix = `${formatLineNumber(index + 1)} `;
|
|
901
|
+
const budget = this.maxReadBytes - Buffer.byteLength(prefix, "utf8");
|
|
902
|
+
const clippedByBytes = clipLineToByteBudget(line, budget);
|
|
903
|
+
if (clippedByBytes.length > 0) {
|
|
904
|
+
outputLines.push(clippedByBytes);
|
|
905
|
+
bytes = Buffer.byteLength(`${prefix}${clippedByBytes}`, "utf8");
|
|
906
|
+
clipped = true;
|
|
907
|
+
if (firstClippedLineNumber === null) {
|
|
908
|
+
firstClippedLineNumber = index + 1;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
832
912
|
truncatedByBytes = true;
|
|
833
913
|
break;
|
|
834
914
|
}
|
|
835
915
|
outputLines.push(line);
|
|
836
916
|
bytes += size;
|
|
837
917
|
}
|
|
838
|
-
const hasMoreLines =
|
|
918
|
+
const hasMoreLines = lines.length > offset + outputLines.length;
|
|
839
919
|
const body = toLineNumbered(outputLines, offset);
|
|
840
920
|
return appendReadSuffix(body, {
|
|
841
921
|
truncatedByBytes,
|
|
842
922
|
hasMoreLines,
|
|
843
923
|
lastReadLine: offset + outputLines.length,
|
|
844
|
-
|
|
845
|
-
clipped
|
|
846
|
-
|
|
924
|
+
maxReadBytes: this.maxReadBytes,
|
|
925
|
+
clipped,
|
|
926
|
+
maxLineLength: this.maxLineLength
|
|
927
|
+
}).concat(
|
|
928
|
+
allowTruncate && firstClippedLineNumber !== null ? `
|
|
929
|
+
|
|
930
|
+
For full long-line content, use tool_output_cache_line({"ref_id":"${normalizeRefId(refId)}","line_number":${firstClippedLineNumber},"char_offset":0,"char_limit":10000}).` : ""
|
|
931
|
+
);
|
|
932
|
+
}
|
|
933
|
+
async readLine(refId, options) {
|
|
934
|
+
const filePath = this.resolvePath(refId);
|
|
935
|
+
const content = await fs5.readFile(filePath, "utf8");
|
|
936
|
+
const lines = content.split(/\r?\n/);
|
|
937
|
+
const lineIndex = options.line_number - 1;
|
|
938
|
+
if (lineIndex < 0 || lineIndex >= lines.length) {
|
|
939
|
+
return `Line number out of range: ${options.line_number} (total ${lines.length})`;
|
|
940
|
+
}
|
|
941
|
+
const line = lines[lineIndex] ?? "";
|
|
942
|
+
const charOffset = Math.max(0, options.char_offset ?? 0);
|
|
943
|
+
const charLimit = Math.max(
|
|
944
|
+
1,
|
|
945
|
+
Math.min(
|
|
946
|
+
Math.trunc(options.char_limit ?? DEFAULT_READ_LINE_CHAR_LIMIT),
|
|
947
|
+
MAX_READ_LINE_CHAR_LIMIT
|
|
948
|
+
)
|
|
949
|
+
);
|
|
950
|
+
if (charOffset > line.length) {
|
|
951
|
+
return `char_offset out of range: ${charOffset} (line length ${line.length})`;
|
|
952
|
+
}
|
|
953
|
+
const segment = line.slice(charOffset, charOffset + charLimit);
|
|
954
|
+
const endOffset = charOffset + segment.length;
|
|
955
|
+
const hasMore = endOffset < line.length;
|
|
956
|
+
let output = [
|
|
957
|
+
`ref_id=${normalizeRefId(refId)}`,
|
|
958
|
+
`line_number=${options.line_number}`,
|
|
959
|
+
`line_length=${line.length}`,
|
|
960
|
+
`char_range=${charOffset}..${Math.max(endOffset - 1, charOffset - 1)}`,
|
|
961
|
+
segment
|
|
962
|
+
].join("\n");
|
|
963
|
+
if (hasMore) {
|
|
964
|
+
output += `
|
|
965
|
+
|
|
966
|
+
Use char_offset=${endOffset} to continue.`;
|
|
967
|
+
output += `
|
|
968
|
+
tool_output_cache_line({"ref_id":"${normalizeRefId(refId)}","line_number":${options.line_number},"char_offset":${endOffset},"char_limit":${charLimit}})`;
|
|
969
|
+
}
|
|
970
|
+
return output;
|
|
847
971
|
}
|
|
848
972
|
async grep(refId, options) {
|
|
849
973
|
const filePath = this.resolvePath(refId);
|
|
@@ -855,7 +979,6 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
855
979
|
const maxMatches = Math.max(1, options.max_matches ?? DEFAULT_MAX_MATCHES);
|
|
856
980
|
const results = [];
|
|
857
981
|
let totalBytes = 0;
|
|
858
|
-
let hasWrappedSnippet = false;
|
|
859
982
|
let hasMoreMatches = false;
|
|
860
983
|
let hasMatch = false;
|
|
861
984
|
for (let i = 0; i < lines.length; i += 1) {
|
|
@@ -868,11 +991,18 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
868
991
|
const start = Math.max(0, i - before);
|
|
869
992
|
const end = Math.min(lines.length, i + after + 1);
|
|
870
993
|
const snippet = renderPhysicalSnippet(lines, start, end);
|
|
871
|
-
if (snippet.wrapped) hasWrappedSnippet = true;
|
|
872
994
|
const rendered = `ref:${normalizeRefId(refId)}
|
|
873
|
-
${snippet
|
|
995
|
+
${snippet}`;
|
|
874
996
|
const size = Buffer.byteLength(rendered, "utf8") + (results.length > 0 ? Buffer.byteLength("\n\n", "utf8") : 0);
|
|
875
|
-
if (totalBytes + size >
|
|
997
|
+
if (totalBytes + size > this.maxGrepBytes) {
|
|
998
|
+
if (results.length === 0) {
|
|
999
|
+
return [
|
|
1000
|
+
`MATCH_TOO_LARGE_TO_RENDER: match at line ${i + 1} exceeded ${this.maxGrepBytes} bytes.`,
|
|
1001
|
+
`ref_id=${normalizeRefId(refId)}`,
|
|
1002
|
+
"Reduce grep context (before/after) or read the matched line directly:",
|
|
1003
|
+
`tool_output_cache_line({"ref_id":"${normalizeRefId(refId)}","line_number":${i + 1},"char_offset":0,"char_limit":10000})`
|
|
1004
|
+
].join("\n");
|
|
1005
|
+
}
|
|
876
1006
|
hasMoreMatches = true;
|
|
877
1007
|
break;
|
|
878
1008
|
}
|
|
@@ -881,7 +1011,7 @@ ${snippet.text}`;
|
|
|
881
1011
|
}
|
|
882
1012
|
if (!results.length) {
|
|
883
1013
|
if (hasMatch) {
|
|
884
|
-
return `Matches found but output exceeded ${
|
|
1014
|
+
return `Matches found but output exceeded ${this.maxGrepBytes} bytes. Reduce context (before/after) and retry.`;
|
|
885
1015
|
}
|
|
886
1016
|
return `No matches for: ${options.pattern}`;
|
|
887
1017
|
}
|
|
@@ -889,11 +1019,6 @@ ${snippet.text}`;
|
|
|
889
1019
|
if (hasMoreMatches) {
|
|
890
1020
|
output += "\n... (truncated)";
|
|
891
1021
|
}
|
|
892
|
-
if (hasWrappedSnippet) {
|
|
893
|
-
output += `
|
|
894
|
-
|
|
895
|
-
Long physical lines are wrapped at ${MAX_LINE_LENGTH} chars per display line.`;
|
|
896
|
-
}
|
|
897
1022
|
return output;
|
|
898
1023
|
}
|
|
899
1024
|
resolvePath(refId) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codelia/storage",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.37",
|
|
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.
|
|
27
|
-
"@codelia/shared-types": "0.1.
|
|
26
|
+
"@codelia/core": "0.1.37",
|
|
27
|
+
"@codelia/shared-types": "0.1.37",
|
|
28
28
|
"better-sqlite3": "^12.6.2"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|