@oyasmi/pipiclaw 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 +31 -0
- package/README.md +247 -0
- package/dist/agent.d.ts +18 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +938 -0
- package/dist/agent.js.map +1 -0
- package/dist/commands.d.ts +9 -0
- package/dist/commands.d.ts.map +1 -0
- package/dist/commands.js +45 -0
- package/dist/commands.js.map +1 -0
- package/dist/context.d.ts +139 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +432 -0
- package/dist/context.js.map +1 -0
- package/dist/delivery.d.ts +4 -0
- package/dist/delivery.d.ts.map +1 -0
- package/dist/delivery.js +221 -0
- package/dist/delivery.js.map +1 -0
- package/dist/dingtalk.d.ts +109 -0
- package/dist/dingtalk.d.ts.map +1 -0
- package/dist/dingtalk.js +655 -0
- package/dist/dingtalk.js.map +1 -0
- package/dist/events.d.ts +51 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +287 -0
- package/dist/events.js.map +1 -0
- package/dist/log.d.ts +33 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +188 -0
- package/dist/log.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +298 -0
- package/dist/main.js.map +1 -0
- package/dist/paths.d.ts +8 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +10 -0
- package/dist/paths.js.map +1 -0
- package/dist/sandbox.d.ts +34 -0
- package/dist/sandbox.d.ts.map +1 -0
- package/dist/sandbox.js +180 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/shell-escape.d.ts +6 -0
- package/dist/shell-escape.d.ts.map +1 -0
- package/dist/shell-escape.js +8 -0
- package/dist/shell-escape.js.map +1 -0
- package/dist/store.d.ts +41 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +110 -0
- package/dist/store.js.map +1 -0
- package/dist/tools/attach.d.ts +14 -0
- package/dist/tools/attach.d.ts.map +1 -0
- package/dist/tools/attach.js +35 -0
- package/dist/tools/attach.js.map +1 -0
- package/dist/tools/bash.d.ts +10 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +78 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/edit.d.ts +11 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +129 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +15 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/read.d.ts +11 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +132 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/truncate.d.ts +57 -0
- package/dist/tools/truncate.d.ts.map +1 -0
- package/dist/tools/truncate.js +184 -0
- package/dist/tools/truncate.js.map +1 -0
- package/dist/tools/write.d.ts +10 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +31 -0
- package/dist/tools/write.js.map +1 -0
- package/package.json +54 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared truncation utilities for tool outputs.
|
|
3
|
+
*
|
|
4
|
+
* Truncation is based on two independent limits - whichever is hit first wins:
|
|
5
|
+
* - Line limit (default: 2000 lines)
|
|
6
|
+
* - Byte limit (default: 50KB)
|
|
7
|
+
*
|
|
8
|
+
* Never returns partial lines (except bash tail truncation edge case).
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_MAX_LINES = 2000;
|
|
11
|
+
export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB
|
|
12
|
+
/**
|
|
13
|
+
* Format bytes as human-readable size.
|
|
14
|
+
*/
|
|
15
|
+
export function formatSize(bytes) {
|
|
16
|
+
if (bytes < 1024) {
|
|
17
|
+
return `${bytes}B`;
|
|
18
|
+
}
|
|
19
|
+
else if (bytes < 1024 * 1024) {
|
|
20
|
+
return `${(bytes / 1024).toFixed(1)}KB`;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Truncate content from the head (keep first N lines/bytes).
|
|
28
|
+
* Suitable for file reads where you want to see the beginning.
|
|
29
|
+
*
|
|
30
|
+
* Never returns partial lines. If first line exceeds byte limit,
|
|
31
|
+
* returns empty content with firstLineExceedsLimit=true.
|
|
32
|
+
*/
|
|
33
|
+
export function truncateHead(content, options = {}) {
|
|
34
|
+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
35
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
36
|
+
const totalBytes = Buffer.byteLength(content, "utf-8");
|
|
37
|
+
const lines = content.split("\n");
|
|
38
|
+
const totalLines = lines.length;
|
|
39
|
+
// Check if no truncation needed
|
|
40
|
+
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
41
|
+
return {
|
|
42
|
+
content,
|
|
43
|
+
truncated: false,
|
|
44
|
+
truncatedBy: null,
|
|
45
|
+
totalLines,
|
|
46
|
+
totalBytes,
|
|
47
|
+
outputLines: totalLines,
|
|
48
|
+
outputBytes: totalBytes,
|
|
49
|
+
lastLinePartial: false,
|
|
50
|
+
firstLineExceedsLimit: false,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// Check if first line alone exceeds byte limit
|
|
54
|
+
const firstLineBytes = Buffer.byteLength(lines[0], "utf-8");
|
|
55
|
+
if (firstLineBytes > maxBytes) {
|
|
56
|
+
return {
|
|
57
|
+
content: "",
|
|
58
|
+
truncated: true,
|
|
59
|
+
truncatedBy: "bytes",
|
|
60
|
+
totalLines,
|
|
61
|
+
totalBytes,
|
|
62
|
+
outputLines: 0,
|
|
63
|
+
outputBytes: 0,
|
|
64
|
+
lastLinePartial: false,
|
|
65
|
+
firstLineExceedsLimit: true,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// Collect complete lines that fit
|
|
69
|
+
const outputLinesArr = [];
|
|
70
|
+
let outputBytesCount = 0;
|
|
71
|
+
let truncatedBy = "lines";
|
|
72
|
+
for (let i = 0; i < lines.length && i < maxLines; i++) {
|
|
73
|
+
const line = lines[i];
|
|
74
|
+
const lineBytes = Buffer.byteLength(line, "utf-8") + (i > 0 ? 1 : 0); // +1 for newline
|
|
75
|
+
if (outputBytesCount + lineBytes > maxBytes) {
|
|
76
|
+
truncatedBy = "bytes";
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
outputLinesArr.push(line);
|
|
80
|
+
outputBytesCount += lineBytes;
|
|
81
|
+
}
|
|
82
|
+
// If we exited due to line limit
|
|
83
|
+
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
84
|
+
truncatedBy = "lines";
|
|
85
|
+
}
|
|
86
|
+
const outputContent = outputLinesArr.join("\n");
|
|
87
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
|
88
|
+
return {
|
|
89
|
+
content: outputContent,
|
|
90
|
+
truncated: true,
|
|
91
|
+
truncatedBy,
|
|
92
|
+
totalLines,
|
|
93
|
+
totalBytes,
|
|
94
|
+
outputLines: outputLinesArr.length,
|
|
95
|
+
outputBytes: finalOutputBytes,
|
|
96
|
+
lastLinePartial: false,
|
|
97
|
+
firstLineExceedsLimit: false,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Truncate content from the tail (keep last N lines/bytes).
|
|
102
|
+
* Suitable for bash output where you want to see the end (errors, final results).
|
|
103
|
+
*
|
|
104
|
+
* May return partial first line if the last line of original content exceeds byte limit.
|
|
105
|
+
*/
|
|
106
|
+
export function truncateTail(content, options = {}) {
|
|
107
|
+
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
108
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
109
|
+
const totalBytes = Buffer.byteLength(content, "utf-8");
|
|
110
|
+
const lines = content.split("\n");
|
|
111
|
+
const totalLines = lines.length;
|
|
112
|
+
// Check if no truncation needed
|
|
113
|
+
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
114
|
+
return {
|
|
115
|
+
content,
|
|
116
|
+
truncated: false,
|
|
117
|
+
truncatedBy: null,
|
|
118
|
+
totalLines,
|
|
119
|
+
totalBytes,
|
|
120
|
+
outputLines: totalLines,
|
|
121
|
+
outputBytes: totalBytes,
|
|
122
|
+
lastLinePartial: false,
|
|
123
|
+
firstLineExceedsLimit: false,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// Work backwards from the end
|
|
127
|
+
const outputLinesArr = [];
|
|
128
|
+
let outputBytesCount = 0;
|
|
129
|
+
let truncatedBy = "lines";
|
|
130
|
+
let lastLinePartial = false;
|
|
131
|
+
for (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {
|
|
132
|
+
const line = lines[i];
|
|
133
|
+
const lineBytes = Buffer.byteLength(line, "utf-8") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline
|
|
134
|
+
if (outputBytesCount + lineBytes > maxBytes) {
|
|
135
|
+
truncatedBy = "bytes";
|
|
136
|
+
// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,
|
|
137
|
+
// take the end of the line (partial)
|
|
138
|
+
if (outputLinesArr.length === 0) {
|
|
139
|
+
const truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);
|
|
140
|
+
outputLinesArr.unshift(truncatedLine);
|
|
141
|
+
outputBytesCount = Buffer.byteLength(truncatedLine, "utf-8");
|
|
142
|
+
lastLinePartial = true;
|
|
143
|
+
}
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
outputLinesArr.unshift(line);
|
|
147
|
+
outputBytesCount += lineBytes;
|
|
148
|
+
}
|
|
149
|
+
// If we exited due to line limit
|
|
150
|
+
if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
|
|
151
|
+
truncatedBy = "lines";
|
|
152
|
+
}
|
|
153
|
+
const outputContent = outputLinesArr.join("\n");
|
|
154
|
+
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
|
155
|
+
return {
|
|
156
|
+
content: outputContent,
|
|
157
|
+
truncated: true,
|
|
158
|
+
truncatedBy,
|
|
159
|
+
totalLines,
|
|
160
|
+
totalBytes,
|
|
161
|
+
outputLines: outputLinesArr.length,
|
|
162
|
+
outputBytes: finalOutputBytes,
|
|
163
|
+
lastLinePartial,
|
|
164
|
+
firstLineExceedsLimit: false,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Truncate a string to fit within a byte limit (from the end).
|
|
169
|
+
* Handles multi-byte UTF-8 characters correctly.
|
|
170
|
+
*/
|
|
171
|
+
function truncateStringToBytesFromEnd(str, maxBytes) {
|
|
172
|
+
const buf = Buffer.from(str, "utf-8");
|
|
173
|
+
if (buf.length <= maxBytes) {
|
|
174
|
+
return str;
|
|
175
|
+
}
|
|
176
|
+
// Start from the end, skip maxBytes back
|
|
177
|
+
let start = buf.length - maxBytes;
|
|
178
|
+
// Find a valid UTF-8 boundary (start of a character)
|
|
179
|
+
while (start < buf.length && (buf[start] & 0xc0) === 0x80) {
|
|
180
|
+
start++;
|
|
181
|
+
}
|
|
182
|
+
return buf.slice(start).toString("utf-8");
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=truncate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../src/tools/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,OAAO;AA8BnD;;GAEG;AACH,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QAClC,WAAW,EAAE,gBAAgB;QAC7B,eAAe;QACf,qBAAqB,EAAE,KAAK;KAC5B,CAAC;AAAA,CACF;AAED;;;GAGG;AACH,SAAS,4BAA4B,CAAC,GAAW,EAAE,QAAgB,EAAU;IAC5E,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACtC,IAAI,GAAG,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC5B,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,yCAAyC;IACzC,IAAI,KAAK,GAAG,GAAG,CAAC,MAAM,GAAG,QAAQ,CAAC;IAElC,qDAAqD;IACrD,OAAO,KAAK,GAAG,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QAC3D,KAAK,EAAE,CAAC;IACT,CAAC;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AAAA,CAC1C","sourcesContent":["/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines (except bash tail truncation edge case).\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = content.split(\"\\n\");\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = Buffer.byteLength(lines[0], \"utf-8\");\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t};\n}\n\n/**\n * Truncate content from the tail (keep last N lines/bytes).\n * Suitable for bash output where you want to see the end (errors, final results).\n *\n * May return partial first line if the last line of original content exceeds byte limit.\n */\nexport function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = content.split(\"\\n\");\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t};\n\t}\n\n\t// Work backwards from the end\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\tlet lastLinePartial = false;\n\n\tfor (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\t// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,\n\t\t\t// take the end of the line (partial)\n\t\t\tif (outputLinesArr.length === 0) {\n\t\t\t\tconst truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);\n\t\t\t\toutputLinesArr.unshift(truncatedLine);\n\t\t\t\toutputBytesCount = Buffer.byteLength(truncatedLine, \"utf-8\");\n\t\t\t\tlastLinePartial = true;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.unshift(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial,\n\t\tfirstLineExceedsLimit: false,\n\t};\n}\n\n/**\n * Truncate a string to fit within a byte limit (from the end).\n * Handles multi-byte UTF-8 characters correctly.\n */\nfunction truncateStringToBytesFromEnd(str: string, maxBytes: number): string {\n\tconst buf = Buffer.from(str, \"utf-8\");\n\tif (buf.length <= maxBytes) {\n\t\treturn str;\n\t}\n\n\t// Start from the end, skip maxBytes back\n\tlet start = buf.length - maxBytes;\n\n\t// Find a valid UTF-8 boundary (start of a character)\n\twhile (start < buf.length && (buf[start] & 0xc0) === 0x80) {\n\t\tstart++;\n\t}\n\n\treturn buf.slice(start).toString(\"utf-8\");\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
|
2
|
+
import type { Executor } from "../sandbox.js";
|
|
3
|
+
declare const writeSchema: import("@sinclair/typebox").TObject<{
|
|
4
|
+
label: import("@sinclair/typebox").TString;
|
|
5
|
+
path: import("@sinclair/typebox").TString;
|
|
6
|
+
content: import("@sinclair/typebox").TString;
|
|
7
|
+
}>;
|
|
8
|
+
export declare function createWriteTool(executor: Executor): AgentTool<typeof writeSchema>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=write.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../../src/tools/write.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE7D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAG9C,QAAA,MAAM,WAAW;;;;EAIf,CAAC;AAEH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC,OAAO,WAAW,CAAC,CA8BjF","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-agent-core\";\nimport { Type } from \"@sinclair/typebox\";\nimport type { Executor } from \"../sandbox.js\";\nimport { shellEscape } from \"../shell-escape.js\";\n\nconst writeSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what you're writing (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to write (relative or absolute)\" }),\n\tcontent: Type.String({ description: \"Content to write to the file\" }),\n});\n\nexport function createWriteTool(executor: Executor): AgentTool<typeof writeSchema> {\n\treturn {\n\t\tname: \"write\",\n\t\tlabel: \"write\",\n\t\tdescription:\n\t\t\t\"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.\",\n\t\tparameters: writeSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ path, content }: { label: string; path: string; content: string },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\t// Create parent directories and write file using heredoc\n\t\t\tconst dir = path.includes(\"/\") ? path.substring(0, path.lastIndexOf(\"/\")) : \".\";\n\n\t\t\t// Use printf to handle content with special characters, pipe to file\n\t\t\t// This avoids issues with heredoc and special characters\n\t\t\tconst cmd = `mkdir -p ${shellEscape(dir)} && printf '%s' ${shellEscape(content)} > ${shellEscape(path)}`;\n\n\t\t\tconst result = await executor.exec(cmd, { signal });\n\t\t\tif (result.code !== 0) {\n\t\t\t\tthrow new Error(result.stderr || `Failed to write file: ${path}`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcontent: [{ type: \"text\", text: `Successfully wrote ${content.length} bytes to ${path}` }],\n\t\t\t\tdetails: undefined,\n\t\t\t};\n\t\t},\n\t};\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import { shellEscape } from "../shell-escape.js";
|
|
3
|
+
const writeSchema = Type.Object({
|
|
4
|
+
label: Type.String({ description: "Brief description of what you're writing (shown to user)" }),
|
|
5
|
+
path: Type.String({ description: "Path to the file to write (relative or absolute)" }),
|
|
6
|
+
content: Type.String({ description: "Content to write to the file" }),
|
|
7
|
+
});
|
|
8
|
+
export function createWriteTool(executor) {
|
|
9
|
+
return {
|
|
10
|
+
name: "write",
|
|
11
|
+
label: "write",
|
|
12
|
+
description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
|
|
13
|
+
parameters: writeSchema,
|
|
14
|
+
execute: async (_toolCallId, { path, content }, signal) => {
|
|
15
|
+
// Create parent directories and write file using heredoc
|
|
16
|
+
const dir = path.includes("/") ? path.substring(0, path.lastIndexOf("/")) : ".";
|
|
17
|
+
// Use printf to handle content with special characters, pipe to file
|
|
18
|
+
// This avoids issues with heredoc and special characters
|
|
19
|
+
const cmd = `mkdir -p ${shellEscape(dir)} && printf '%s' ${shellEscape(content)} > ${shellEscape(path)}`;
|
|
20
|
+
const result = await executor.exec(cmd, { signal });
|
|
21
|
+
if (result.code !== 0) {
|
|
22
|
+
throw new Error(result.stderr || `Failed to write file: ${path}`);
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
content: [{ type: "text", text: `Successfully wrote ${content.length} bytes to ${path}` }],
|
|
26
|
+
details: undefined,
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=write.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write.js","sourceRoot":"","sources":["../../src/tools/write.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC;IAC/B,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,0DAA0D,EAAE,CAAC;IAC/F,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,kDAAkD,EAAE,CAAC;IACtF,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,8BAA8B,EAAE,CAAC;CACrE,CAAC,CAAC;AAEH,MAAM,UAAU,eAAe,CAAC,QAAkB,EAAiC;IAClF,OAAO;QACN,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,OAAO;QACd,WAAW,EACV,iIAAiI;QAClI,UAAU,EAAE,WAAW;QACvB,OAAO,EAAE,KAAK,EACb,WAAmB,EACnB,EAAE,IAAI,EAAE,OAAO,EAAoD,EACnE,MAAoB,EACnB,EAAE,CAAC;YACJ,yDAAyD;YACzD,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;YAEhF,qEAAqE;YACrE,yDAAyD;YACzD,MAAM,GAAG,GAAG,YAAY,WAAW,CAAC,GAAG,CAAC,mBAAmB,WAAW,CAAC,OAAO,CAAC,MAAM,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YAEzG,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;YACpD,IAAI,MAAM,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBACvB,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,IAAI,yBAAyB,IAAI,EAAE,CAAC,CAAC;YACnE,CAAC;YAED,OAAO;gBACN,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,sBAAsB,OAAO,CAAC,MAAM,aAAa,IAAI,EAAE,EAAE,CAAC;gBAC1F,OAAO,EAAE,SAAS;aAClB,CAAC;QAAA,CACF;KACD,CAAC;AAAA,CACF","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-agent-core\";\nimport { Type } from \"@sinclair/typebox\";\nimport type { Executor } from \"../sandbox.js\";\nimport { shellEscape } from \"../shell-escape.js\";\n\nconst writeSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what you're writing (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to write (relative or absolute)\" }),\n\tcontent: Type.String({ description: \"Content to write to the file\" }),\n});\n\nexport function createWriteTool(executor: Executor): AgentTool<typeof writeSchema> {\n\treturn {\n\t\tname: \"write\",\n\t\tlabel: \"write\",\n\t\tdescription:\n\t\t\t\"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.\",\n\t\tparameters: writeSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ path, content }: { label: string; path: string; content: string },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\t// Create parent directories and write file using heredoc\n\t\t\tconst dir = path.includes(\"/\") ? path.substring(0, path.lastIndexOf(\"/\")) : \".\";\n\n\t\t\t// Use printf to handle content with special characters, pipe to file\n\t\t\t// This avoids issues with heredoc and special characters\n\t\t\tconst cmd = `mkdir -p ${shellEscape(dir)} && printf '%s' ${shellEscape(content)} > ${shellEscape(path)}`;\n\n\t\t\tconst result = await executor.exec(cmd, { signal });\n\t\t\tif (result.code !== 0) {\n\t\t\t\tthrow new Error(result.stderr || `Failed to write file: ${path}`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcontent: [{ type: \"text\", text: `Successfully wrote ${content.length} bytes to ${path}` }],\n\t\t\t\tdetails: undefined,\n\t\t\t};\n\t\t},\n\t};\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oyasmi/pipiclaw",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pipiclaw DingTalk bot powered by the pi coding agent",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"pipiclaw": "dist/main.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"CHANGELOG.md",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"clean": "rm -rf dist",
|
|
16
|
+
"build": "tsgo -p tsconfig.build.json && chmod +x dist/main.js",
|
|
17
|
+
"dev": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
|
|
18
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@mariozechner/pi-agent-core": "^0.55.1",
|
|
22
|
+
"@mariozechner/pi-ai": "^0.55.1",
|
|
23
|
+
"@mariozechner/pi-coding-agent": "^0.55.1",
|
|
24
|
+
"@sinclair/typebox": "^0.34.0",
|
|
25
|
+
"axios": "^1.7.0",
|
|
26
|
+
"chalk": "^5.6.2",
|
|
27
|
+
"croner": "^9.1.0",
|
|
28
|
+
"diff": "^8.0.2",
|
|
29
|
+
"dingtalk-stream": "^2.1.4"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/diff": "^7.0.2",
|
|
33
|
+
"@types/node": "^24.3.0",
|
|
34
|
+
"typescript": "^5.7.3"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"dingtalk",
|
|
38
|
+
"bot",
|
|
39
|
+
"ai",
|
|
40
|
+
"agent"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/badlogic/pi-mono.git",
|
|
46
|
+
"directory": "packages/pipiclaw"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=20.0.0"
|
|
53
|
+
}
|
|
54
|
+
}
|