agent-repl 0.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.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +167 -0
- package/dist/cli.js +122 -0
- package/dist/host.js +24 -0
- package/dist/mcp.js +109 -0
- package/dist/pi-compat/LICENSE +21 -0
- package/dist/pi-compat/child-process.js +105 -0
- package/dist/pi-compat/exec.js +76 -0
- package/dist/pi-compat/index.js +3 -0
- package/dist/pi-compat/string-enum.js +22 -0
- package/dist/pi-compat/truncate.js +216 -0
- package/dist/pi-repl.js +2583 -0
- package/dist/shared/REPL_SESSION_RECORD_PROTOCOL.md +105 -0
- package/dist/shared/repl-control-files.js +170 -0
- package/dist/shared/repl-cpp-driver.hxx +164 -0
- package/dist/shared/repl-cpp.js +107 -0
- package/dist/shared/repl-gnuplot-processes.js +180 -0
- package/dist/shared/repl-gnuplot.js +58 -0
- package/dist/shared/repl-history.js +37 -0
- package/dist/shared/repl-m-language.js +47 -0
- package/dist/shared/repl-session-record.js +623 -0
- package/dist/shared/repl-session-stop.js +278 -0
- package/dist/shared/repl-submission-display.js +249 -0
- package/dist/text.js +20 -0
- package/package.json +59 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copied from @earendil-works/pi-ai dist/utils/typebox-helpers 0.85.1 (MIT License, (c) 2025 Mario Zechner; see LICENSE).
|
|
2
|
+
// Regenerated by scripts/sync-from-pi-repl.mjs; do not edit by hand.
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a string enum schema compatible with Google's API and other providers
|
|
6
|
+
* that don't support anyOf/const patterns.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const OperationSchema = StringEnum(["add", "subtract", "multiply", "divide"], {
|
|
10
|
+
* description: "The operation to perform"
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
* type Operation = Static<typeof OperationSchema>; // "add" | "subtract" | "multiply" | "divide"
|
|
14
|
+
*/
|
|
15
|
+
export function StringEnum(values, options) {
|
|
16
|
+
return Type.Unsafe({
|
|
17
|
+
type: "string",
|
|
18
|
+
enum: values,
|
|
19
|
+
...(options?.description && { description: options.description }),
|
|
20
|
+
...(options?.default && { default: options.default }),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Copied from @earendil-works/pi-coding-agent dist/core/tools/truncate 0.85.1 (MIT License, (c) 2025 Mario Zechner; see LICENSE).
|
|
2
|
+
// Regenerated by scripts/sync-from-pi-repl.mjs; do not edit by hand.
|
|
3
|
+
/**
|
|
4
|
+
* Shared truncation utilities for tool outputs.
|
|
5
|
+
*
|
|
6
|
+
* Truncation is based on two independent limits - whichever is hit first wins:
|
|
7
|
+
* - Line limit (default: 2000 lines)
|
|
8
|
+
* - Byte limit (default: 50KB)
|
|
9
|
+
*
|
|
10
|
+
* Never returns partial lines (except bash tail truncation edge case).
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_MAX_LINES = 2000;
|
|
13
|
+
export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB
|
|
14
|
+
export const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line
|
|
15
|
+
function splitLinesForCounting(content) {
|
|
16
|
+
if (content.length === 0) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
const lines = content.split("\n");
|
|
20
|
+
if (content.endsWith("\n")) {
|
|
21
|
+
lines.pop();
|
|
22
|
+
}
|
|
23
|
+
return lines;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Format bytes as human-readable size.
|
|
27
|
+
*/
|
|
28
|
+
export function formatSize(bytes) {
|
|
29
|
+
if (bytes < 1024) {
|
|
30
|
+
return `${bytes}B`;
|
|
31
|
+
}
|
|
32
|
+
else if (bytes < 1024 * 1024) {
|
|
33
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Truncate content from the head (keep first N lines/bytes).
|
|
41
|
+
* Suitable for file reads where you want to see the beginning.
|
|
42
|
+
*
|
|
43
|
+
* Never returns partial lines. If first line exceeds byte limit,
|
|
44
|
+
* returns empty content with firstLineExceedsLimit=true.
|
|
45
|
+
*/
|
|
46
|
+
export function truncateHead(content, options = {}) {
|
|
47
|
+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
48
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
49
|
+
const totalBytes = Buffer.byteLength(content, "utf-8");
|
|
50
|
+
const lines = splitLinesForCounting(content);
|
|
51
|
+
const totalLines = lines.length;
|
|
52
|
+
// Check if no truncation needed
|
|
53
|
+
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
54
|
+
return {
|
|
55
|
+
content,
|
|
56
|
+
truncated: false,
|
|
57
|
+
truncatedBy: null,
|
|
58
|
+
totalLines,
|
|
59
|
+
totalBytes,
|
|
60
|
+
outputLines: totalLines,
|
|
61
|
+
outputBytes: totalBytes,
|
|
62
|
+
lastLinePartial: false,
|
|
63
|
+
firstLineExceedsLimit: false,
|
|
64
|
+
maxLines,
|
|
65
|
+
maxBytes,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// Check if first line alone exceeds byte limit
|
|
69
|
+
const firstLineBytes = Buffer.byteLength(lines[0], "utf-8");
|
|
70
|
+
if (firstLineBytes > maxBytes) {
|
|
71
|
+
return {
|
|
72
|
+
content: "",
|
|
73
|
+
truncated: true,
|
|
74
|
+
truncatedBy: "bytes",
|
|
75
|
+
totalLines,
|
|
76
|
+
totalBytes,
|
|
77
|
+
outputLines: 0,
|
|
78
|
+
outputBytes: 0,
|
|
79
|
+
lastLinePartial: false,
|
|
80
|
+
firstLineExceedsLimit: true,
|
|
81
|
+
maxLines,
|
|
82
|
+
maxBytes,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Collect complete lines that fit
|
|
86
|
+
const outputLinesArr = [];
|
|
87
|
+
let outputBytesCount = 0;
|
|
88
|
+
let truncatedBy = "lines";
|
|
89
|
+
for (let i = 0; i < lines.length && i < maxLines; i++) {
|
|
90
|
+
const line = lines[i];
|
|
91
|
+
const lineBytes = Buffer.byteLength(line, "utf-8") + (i > 0 ? 1 : 0); // +1 for newline
|
|
92
|
+
if (outputBytesCount + lineBytes > maxBytes) {
|
|
93
|
+
truncatedBy = "bytes";
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
outputLinesArr.push(line);
|
|
97
|
+
outputBytesCount += lineBytes;
|
|
98
|
+
}
|
|
99
|
+
// If we exited due to line limit
|
|
100
|
+
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
101
|
+
truncatedBy = "lines";
|
|
102
|
+
}
|
|
103
|
+
const outputContent = outputLinesArr.join("\n");
|
|
104
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
|
105
|
+
return {
|
|
106
|
+
content: outputContent,
|
|
107
|
+
truncated: true,
|
|
108
|
+
truncatedBy,
|
|
109
|
+
totalLines,
|
|
110
|
+
totalBytes,
|
|
111
|
+
outputLines: outputLinesArr.length,
|
|
112
|
+
outputBytes: finalOutputBytes,
|
|
113
|
+
lastLinePartial: false,
|
|
114
|
+
firstLineExceedsLimit: false,
|
|
115
|
+
maxLines,
|
|
116
|
+
maxBytes,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Truncate content from the tail (keep last N lines/bytes).
|
|
121
|
+
* Suitable for bash output where you want to see the end (errors, final results).
|
|
122
|
+
*
|
|
123
|
+
* May return partial first line if the last line of original content exceeds byte limit.
|
|
124
|
+
*/
|
|
125
|
+
export function truncateTail(content, options = {}) {
|
|
126
|
+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
127
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
128
|
+
const totalBytes = Buffer.byteLength(content, "utf-8");
|
|
129
|
+
const lines = splitLinesForCounting(content);
|
|
130
|
+
const totalLines = lines.length;
|
|
131
|
+
// Check if no truncation needed
|
|
132
|
+
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
133
|
+
return {
|
|
134
|
+
content,
|
|
135
|
+
truncated: false,
|
|
136
|
+
truncatedBy: null,
|
|
137
|
+
totalLines,
|
|
138
|
+
totalBytes,
|
|
139
|
+
outputLines: totalLines,
|
|
140
|
+
outputBytes: totalBytes,
|
|
141
|
+
lastLinePartial: false,
|
|
142
|
+
firstLineExceedsLimit: false,
|
|
143
|
+
maxLines,
|
|
144
|
+
maxBytes,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
// Work backwards from the end
|
|
148
|
+
const outputLinesArr = [];
|
|
149
|
+
let outputBytesCount = 0;
|
|
150
|
+
let truncatedBy = "lines";
|
|
151
|
+
let lastLinePartial = false;
|
|
152
|
+
for (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {
|
|
153
|
+
const line = lines[i];
|
|
154
|
+
const lineBytes = Buffer.byteLength(line, "utf-8") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline
|
|
155
|
+
if (outputBytesCount + lineBytes > maxBytes) {
|
|
156
|
+
truncatedBy = "bytes";
|
|
157
|
+
// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,
|
|
158
|
+
// take the end of the line (partial)
|
|
159
|
+
if (outputLinesArr.length === 0) {
|
|
160
|
+
const truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);
|
|
161
|
+
outputLinesArr.unshift(truncatedLine);
|
|
162
|
+
outputBytesCount = Buffer.byteLength(truncatedLine, "utf-8");
|
|
163
|
+
lastLinePartial = true;
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
outputLinesArr.unshift(line);
|
|
168
|
+
outputBytesCount += lineBytes;
|
|
169
|
+
}
|
|
170
|
+
// If we exited due to line limit
|
|
171
|
+
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
172
|
+
truncatedBy = "lines";
|
|
173
|
+
}
|
|
174
|
+
const outputContent = outputLinesArr.join("\n");
|
|
175
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
|
176
|
+
return {
|
|
177
|
+
content: outputContent,
|
|
178
|
+
truncated: true,
|
|
179
|
+
truncatedBy,
|
|
180
|
+
totalLines,
|
|
181
|
+
totalBytes,
|
|
182
|
+
outputLines: outputLinesArr.length,
|
|
183
|
+
outputBytes: finalOutputBytes,
|
|
184
|
+
lastLinePartial,
|
|
185
|
+
firstLineExceedsLimit: false,
|
|
186
|
+
maxLines,
|
|
187
|
+
maxBytes,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Truncate a string to fit within a byte limit (from the end).
|
|
192
|
+
* Handles multi-byte UTF-8 characters correctly.
|
|
193
|
+
*/
|
|
194
|
+
function truncateStringToBytesFromEnd(str, maxBytes) {
|
|
195
|
+
const buf = Buffer.from(str, "utf-8");
|
|
196
|
+
if (buf.length <= maxBytes) {
|
|
197
|
+
return str;
|
|
198
|
+
}
|
|
199
|
+
// Start from the end, skip maxBytes back
|
|
200
|
+
let start = buf.length - maxBytes;
|
|
201
|
+
// Find a valid UTF-8 boundary (start of a character)
|
|
202
|
+
while (start < buf.length && (buf[start] & 0xc0) === 0x80) {
|
|
203
|
+
start++;
|
|
204
|
+
}
|
|
205
|
+
return buf.slice(start).toString("utf-8");
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Truncate a single line to max characters, adding [truncated] suffix.
|
|
209
|
+
* Used for grep match lines.
|
|
210
|
+
*/
|
|
211
|
+
export function truncateLine(line, maxChars = GREP_MAX_LINE_LENGTH) {
|
|
212
|
+
if (line.length <= maxChars) {
|
|
213
|
+
return { text: line, wasTruncated: false };
|
|
214
|
+
}
|
|
215
|
+
return { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };
|
|
216
|
+
}
|