@codelia/storage 0.1.35 → 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 +248 -8
- package/dist/index.d.cts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +248 -8
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -771,14 +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 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;
|
|
774
779
|
var normalizeRefId = (refId) => refId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
775
780
|
var formatLineNumber = (lineNumber) => String(lineNumber).padStart(5, " ");
|
|
776
781
|
var toLineNumbered = (lines, offset) => lines.map((line, index) => `${formatLineNumber(index + offset + 1)} ${line}`).join("\n");
|
|
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;
|
|
827
|
+
}
|
|
828
|
+
return out;
|
|
829
|
+
};
|
|
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}`;
|
|
839
|
+
};
|
|
840
|
+
var appendReadSuffix = (output, options) => {
|
|
841
|
+
let next = output;
|
|
842
|
+
if (options.truncatedByBytes || options.hasMoreLines) {
|
|
843
|
+
const reason = options.truncatedByBytes ? `Output truncated at ${options.maxReadBytes} bytes.` : "Output has more lines.";
|
|
844
|
+
next += `
|
|
845
|
+
|
|
846
|
+
${reason} Use offset to read beyond line ${options.lastReadLine}.`;
|
|
847
|
+
}
|
|
848
|
+
if (options.clipped) {
|
|
849
|
+
next += `
|
|
850
|
+
|
|
851
|
+
Long physical lines are clipped at ${options.maxLineLength} chars.`;
|
|
852
|
+
}
|
|
853
|
+
return next;
|
|
854
|
+
};
|
|
855
|
+
var renderPhysicalSnippet = (physicalLines, start, end) => {
|
|
856
|
+
const output = [];
|
|
857
|
+
for (let index = start; index < end; index += 1) {
|
|
858
|
+
output.push(`${formatLineNumber(index + 1)} ${physicalLines[index] ?? ""}`);
|
|
859
|
+
}
|
|
860
|
+
return output.join("\n");
|
|
861
|
+
};
|
|
777
862
|
var ToolOutputCacheStoreImpl = class {
|
|
778
863
|
baseDir;
|
|
864
|
+
maxLineLength;
|
|
865
|
+
maxReadBytes;
|
|
866
|
+
maxGrepBytes;
|
|
779
867
|
constructor(options = {}) {
|
|
780
868
|
const paths = options.paths ?? resolveStoragePaths();
|
|
781
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
|
+
);
|
|
782
882
|
}
|
|
783
883
|
async save(record) {
|
|
784
884
|
const refId = normalizeRefId(record.tool_call_id);
|
|
@@ -795,11 +895,123 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
795
895
|
const lines = content.split(/\r?\n/);
|
|
796
896
|
const offset = options.offset ?? 0;
|
|
797
897
|
const limit = options.limit ?? lines.length;
|
|
898
|
+
const allowTruncate = options.allow_truncate ?? false;
|
|
798
899
|
if (offset >= lines.length) {
|
|
799
900
|
return "Offset exceeds output length.";
|
|
800
901
|
}
|
|
801
|
-
const
|
|
802
|
-
|
|
902
|
+
const outputLines = [];
|
|
903
|
+
let bytes = 0;
|
|
904
|
+
let truncatedByBytes = false;
|
|
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
|
+
}
|
|
924
|
+
const numberedLine = `${formatLineNumber(index + 1)} ${line}`;
|
|
925
|
+
const size = Buffer.byteLength(numberedLine, "utf8") + (outputLines.length > 0 ? 1 : 0);
|
|
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
|
+
}
|
|
956
|
+
truncatedByBytes = true;
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
outputLines.push(line);
|
|
960
|
+
bytes += size;
|
|
961
|
+
}
|
|
962
|
+
const hasMoreLines = lines.length > offset + outputLines.length;
|
|
963
|
+
const body = toLineNumbered(outputLines, offset);
|
|
964
|
+
return appendReadSuffix(body, {
|
|
965
|
+
truncatedByBytes,
|
|
966
|
+
hasMoreLines,
|
|
967
|
+
lastReadLine: offset + outputLines.length,
|
|
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;
|
|
803
1015
|
}
|
|
804
1016
|
async grep(refId, options) {
|
|
805
1017
|
const filePath = this.resolvePath(refId);
|
|
@@ -810,20 +1022,48 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
810
1022
|
const after = Math.max(0, options.after ?? 0);
|
|
811
1023
|
const maxMatches = Math.max(1, options.max_matches ?? DEFAULT_MAX_MATCHES);
|
|
812
1024
|
const results = [];
|
|
1025
|
+
let totalBytes = 0;
|
|
1026
|
+
let hasMoreMatches = false;
|
|
1027
|
+
let hasMatch = false;
|
|
813
1028
|
for (let i = 0; i < lines.length; i += 1) {
|
|
814
|
-
if (results.length >= maxMatches)
|
|
1029
|
+
if (results.length >= maxMatches) {
|
|
1030
|
+
hasMoreMatches = true;
|
|
1031
|
+
break;
|
|
1032
|
+
}
|
|
815
1033
|
if (!regex.test(lines[i])) continue;
|
|
1034
|
+
hasMatch = true;
|
|
816
1035
|
const start = Math.max(0, i - before);
|
|
817
1036
|
const end = Math.min(lines.length, i + after + 1);
|
|
818
|
-
const snippet =
|
|
819
|
-
|
|
820
|
-
${snippet}
|
|
1037
|
+
const snippet = renderPhysicalSnippet(lines, start, end);
|
|
1038
|
+
const rendered = `ref:${normalizeRefId(refId)}
|
|
1039
|
+
${snippet}`;
|
|
1040
|
+
const size = Buffer.byteLength(rendered, "utf8") + (results.length > 0 ? Buffer.byteLength("\n\n", "utf8") : 0);
|
|
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
|
+
}
|
|
1050
|
+
hasMoreMatches = true;
|
|
1051
|
+
break;
|
|
1052
|
+
}
|
|
1053
|
+
results.push(rendered);
|
|
1054
|
+
totalBytes += size;
|
|
821
1055
|
}
|
|
822
1056
|
if (!results.length) {
|
|
1057
|
+
if (hasMatch) {
|
|
1058
|
+
return `Matches found but output exceeded ${this.maxGrepBytes} bytes. Reduce context (before/after) and retry.`;
|
|
1059
|
+
}
|
|
823
1060
|
return `No matches for: ${options.pattern}`;
|
|
824
1061
|
}
|
|
825
|
-
|
|
826
|
-
|
|
1062
|
+
let output = results.join("\n\n");
|
|
1063
|
+
if (hasMoreMatches) {
|
|
1064
|
+
output += "\n... (truncated)";
|
|
1065
|
+
}
|
|
1066
|
+
return output;
|
|
827
1067
|
}
|
|
828
1068
|
resolvePath(refId) {
|
|
829
1069
|
return import_node_path5.default.join(this.baseDir, `${normalizeRefId(refId)}.txt`);
|
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,14 +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 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;
|
|
730
735
|
var normalizeRefId = (refId) => refId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
731
736
|
var formatLineNumber = (lineNumber) => String(lineNumber).padStart(5, " ");
|
|
732
737
|
var toLineNumbered = (lines, offset) => lines.map((line, index) => `${formatLineNumber(index + offset + 1)} ${line}`).join("\n");
|
|
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;
|
|
783
|
+
}
|
|
784
|
+
return out;
|
|
785
|
+
};
|
|
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}`;
|
|
795
|
+
};
|
|
796
|
+
var appendReadSuffix = (output, options) => {
|
|
797
|
+
let next = output;
|
|
798
|
+
if (options.truncatedByBytes || options.hasMoreLines) {
|
|
799
|
+
const reason = options.truncatedByBytes ? `Output truncated at ${options.maxReadBytes} bytes.` : "Output has more lines.";
|
|
800
|
+
next += `
|
|
801
|
+
|
|
802
|
+
${reason} Use offset to read beyond line ${options.lastReadLine}.`;
|
|
803
|
+
}
|
|
804
|
+
if (options.clipped) {
|
|
805
|
+
next += `
|
|
806
|
+
|
|
807
|
+
Long physical lines are clipped at ${options.maxLineLength} chars.`;
|
|
808
|
+
}
|
|
809
|
+
return next;
|
|
810
|
+
};
|
|
811
|
+
var renderPhysicalSnippet = (physicalLines, start, end) => {
|
|
812
|
+
const output = [];
|
|
813
|
+
for (let index = start; index < end; index += 1) {
|
|
814
|
+
output.push(`${formatLineNumber(index + 1)} ${physicalLines[index] ?? ""}`);
|
|
815
|
+
}
|
|
816
|
+
return output.join("\n");
|
|
817
|
+
};
|
|
733
818
|
var ToolOutputCacheStoreImpl = class {
|
|
734
819
|
baseDir;
|
|
820
|
+
maxLineLength;
|
|
821
|
+
maxReadBytes;
|
|
822
|
+
maxGrepBytes;
|
|
735
823
|
constructor(options = {}) {
|
|
736
824
|
const paths = options.paths ?? resolveStoragePaths();
|
|
737
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
|
+
);
|
|
738
838
|
}
|
|
739
839
|
async save(record) {
|
|
740
840
|
const refId = normalizeRefId(record.tool_call_id);
|
|
@@ -751,11 +851,123 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
751
851
|
const lines = content.split(/\r?\n/);
|
|
752
852
|
const offset = options.offset ?? 0;
|
|
753
853
|
const limit = options.limit ?? lines.length;
|
|
854
|
+
const allowTruncate = options.allow_truncate ?? false;
|
|
754
855
|
if (offset >= lines.length) {
|
|
755
856
|
return "Offset exceeds output length.";
|
|
756
857
|
}
|
|
757
|
-
const
|
|
758
|
-
|
|
858
|
+
const outputLines = [];
|
|
859
|
+
let bytes = 0;
|
|
860
|
+
let truncatedByBytes = false;
|
|
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
|
+
}
|
|
880
|
+
const numberedLine = `${formatLineNumber(index + 1)} ${line}`;
|
|
881
|
+
const size = Buffer.byteLength(numberedLine, "utf8") + (outputLines.length > 0 ? 1 : 0);
|
|
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
|
+
}
|
|
912
|
+
truncatedByBytes = true;
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
outputLines.push(line);
|
|
916
|
+
bytes += size;
|
|
917
|
+
}
|
|
918
|
+
const hasMoreLines = lines.length > offset + outputLines.length;
|
|
919
|
+
const body = toLineNumbered(outputLines, offset);
|
|
920
|
+
return appendReadSuffix(body, {
|
|
921
|
+
truncatedByBytes,
|
|
922
|
+
hasMoreLines,
|
|
923
|
+
lastReadLine: offset + outputLines.length,
|
|
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;
|
|
759
971
|
}
|
|
760
972
|
async grep(refId, options) {
|
|
761
973
|
const filePath = this.resolvePath(refId);
|
|
@@ -766,20 +978,48 @@ var ToolOutputCacheStoreImpl = class {
|
|
|
766
978
|
const after = Math.max(0, options.after ?? 0);
|
|
767
979
|
const maxMatches = Math.max(1, options.max_matches ?? DEFAULT_MAX_MATCHES);
|
|
768
980
|
const results = [];
|
|
981
|
+
let totalBytes = 0;
|
|
982
|
+
let hasMoreMatches = false;
|
|
983
|
+
let hasMatch = false;
|
|
769
984
|
for (let i = 0; i < lines.length; i += 1) {
|
|
770
|
-
if (results.length >= maxMatches)
|
|
985
|
+
if (results.length >= maxMatches) {
|
|
986
|
+
hasMoreMatches = true;
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
771
989
|
if (!regex.test(lines[i])) continue;
|
|
990
|
+
hasMatch = true;
|
|
772
991
|
const start = Math.max(0, i - before);
|
|
773
992
|
const end = Math.min(lines.length, i + after + 1);
|
|
774
|
-
const snippet =
|
|
775
|
-
|
|
776
|
-
${snippet}
|
|
993
|
+
const snippet = renderPhysicalSnippet(lines, start, end);
|
|
994
|
+
const rendered = `ref:${normalizeRefId(refId)}
|
|
995
|
+
${snippet}`;
|
|
996
|
+
const size = Buffer.byteLength(rendered, "utf8") + (results.length > 0 ? Buffer.byteLength("\n\n", "utf8") : 0);
|
|
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
|
+
}
|
|
1006
|
+
hasMoreMatches = true;
|
|
1007
|
+
break;
|
|
1008
|
+
}
|
|
1009
|
+
results.push(rendered);
|
|
1010
|
+
totalBytes += size;
|
|
777
1011
|
}
|
|
778
1012
|
if (!results.length) {
|
|
1013
|
+
if (hasMatch) {
|
|
1014
|
+
return `Matches found but output exceeded ${this.maxGrepBytes} bytes. Reduce context (before/after) and retry.`;
|
|
1015
|
+
}
|
|
779
1016
|
return `No matches for: ${options.pattern}`;
|
|
780
1017
|
}
|
|
781
|
-
|
|
782
|
-
|
|
1018
|
+
let output = results.join("\n\n");
|
|
1019
|
+
if (hasMoreMatches) {
|
|
1020
|
+
output += "\n... (truncated)";
|
|
1021
|
+
}
|
|
1022
|
+
return output;
|
|
783
1023
|
}
|
|
784
1024
|
resolvePath(refId) {
|
|
785
1025
|
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.
|
|
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": {
|