opencode-fs-plugin 3.4.0 → 3.6.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/README.md +268 -132
- package/SPEC.md +171 -56
- package/package.json +2 -2
- package/skills/filesystem/SKILL.md +172 -67
- package/src/core/format.ts +3 -9
- package/src/core/fsutil.ts +84 -23
- package/src/core/jobs.ts +210 -34
- package/src/core/permissions.ts +32 -14
- package/src/core/relay-control.ts +343 -0
- package/src/core/relay-protocol.ts +274 -0
- package/src/core/safe-output.ts +290 -0
- package/src/core/sandbox.ts +43 -0
- package/src/core/types.ts +0 -5
- package/src/index.ts +78 -72
- package/src/tools/ar.ts +62 -29
- package/src/tools/cat.ts +31 -46
- package/src/tools/chmod.ts +30 -19
- package/src/tools/chown.ts +24 -16
- package/src/tools/command.ts +536 -0
- package/src/tools/cp.ts +84 -20
- package/src/tools/cpio.ts +204 -65
- package/src/tools/{http_request.ts → curl.ts} +127 -10
- package/src/tools/dd.ts +15 -9
- package/src/tools/diff.ts +46 -15
- package/src/tools/grep.ts +5 -2
- package/src/tools/gzip.ts +13 -15
- package/src/tools/mktemp.ts +41 -26
- package/src/tools/mv.ts +69 -28
- package/src/tools/pipe.ts +24 -16
- package/src/tools/split.ts +26 -12
- package/src/tools/tar.ts +132 -57
- package/src/tools/tee.ts +7 -4
- package/src/tools/touch.ts +12 -4
- package/src/tools/truncate.ts +17 -7
- package/src/tools/zip.ts +119 -54
- package/src/core/spill.ts +0 -145
- package/src/tools/exec.ts +0 -483
package/src/tools/truncate.ts
CHANGED
|
@@ -36,21 +36,31 @@ export const truncateTool: ToolSpec<Input> = {
|
|
|
36
36
|
if (input.size === undefined && input.reference === undefined) {
|
|
37
37
|
throw new FsError("size or reference is required.", "invalid");
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
let referenceSize: number | undefined;
|
|
40
|
+
if (input.reference !== undefined) {
|
|
41
|
+
const reference = await ctx.sandbox.resolveFollow(input.reference);
|
|
42
|
+
await ctx.permission.assert([{ target: reference, boundary: ctx.sandbox.boundaryFor(reference.full, "file") }]);
|
|
43
|
+
const stats = await ctx.sandbox.lstat(reference.full, input.reference);
|
|
44
|
+
referenceSize = num(stats.size);
|
|
45
|
+
}
|
|
43
46
|
|
|
44
47
|
const changed: string[] = [];
|
|
45
48
|
for (const requested of input.paths) {
|
|
46
|
-
const target =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
const target = input.noDereference === true
|
|
50
|
+
? await ctx.sandbox.resolveForCreate(requested)
|
|
51
|
+
: await ctx.sandbox.resolveForWrite(requested);
|
|
52
|
+
let stats: Awaited<ReturnType<typeof fs.lstat>> | undefined;
|
|
49
53
|
try {
|
|
50
54
|
stats = await ctx.sandbox.lstat(target.full, requested);
|
|
51
55
|
} catch (error) {
|
|
52
56
|
/* v8 ignore next 2 */
|
|
53
57
|
if (!(error instanceof FsError) || error.code !== "not-found") throw error;
|
|
58
|
+
}
|
|
59
|
+
await ctx.permission.assert([{
|
|
60
|
+
target,
|
|
61
|
+
boundary: ctx.sandbox.boundaryFor(target.full, stats?.isDirectory() === true ? "directory" : "file"),
|
|
62
|
+
}]);
|
|
63
|
+
if (stats === undefined) {
|
|
54
64
|
/* v8 ignore next */
|
|
55
65
|
if (input.noCreate === true) continue;
|
|
56
66
|
/* v8 ignore next 10 */
|
package/src/tools/zip.ts
CHANGED
|
@@ -9,6 +9,22 @@ import { ZipFile as ZipWriter } from "yazl";
|
|
|
9
9
|
import * as yauzl from "yauzl";
|
|
10
10
|
import { Type, type Static } from "@sinclair/typebox";
|
|
11
11
|
import { FsError, fromErrno } from "../core/errors.js";
|
|
12
|
+
import { walkAll } from "../core/fsutil.js";
|
|
13
|
+
import { mapLimit } from "../core/parallel.js";
|
|
14
|
+
import {
|
|
15
|
+
assertArchivePlan,
|
|
16
|
+
assertArchiveSymlinkPlan,
|
|
17
|
+
assertSafeLinkTarget,
|
|
18
|
+
chmodDirectoryNoFollow,
|
|
19
|
+
finalDirectoryPlans,
|
|
20
|
+
outputBoundary,
|
|
21
|
+
resolveArchiveTarget,
|
|
22
|
+
resolveOutputTarget,
|
|
23
|
+
safeDirectoryMode,
|
|
24
|
+
safeMode,
|
|
25
|
+
throwIfAborted,
|
|
26
|
+
writeOutput,
|
|
27
|
+
} from "../core/safe-output.js";
|
|
12
28
|
import { collect } from "../core/stream.js";
|
|
13
29
|
import { textResult, type ToolSpec } from "../core/types.js";
|
|
14
30
|
|
|
@@ -48,7 +64,7 @@ export const zipTool: ToolSpec<ZipInput> = {
|
|
|
48
64
|
pipeable: "sink",
|
|
49
65
|
mutates: true,
|
|
50
66
|
async run(ctx, input, io) {
|
|
51
|
-
const archive = await ctx.sandbox
|
|
67
|
+
const archive = await resolveOutputTarget(ctx.sandbox, input.archive);
|
|
52
68
|
const requestedPaths = input.paths ?? [];
|
|
53
69
|
if (requestedPaths.length === 0) {
|
|
54
70
|
if (input.input === undefined && io.stdin === undefined) {
|
|
@@ -59,7 +75,7 @@ export const zipTool: ToolSpec<ZipInput> = {
|
|
|
59
75
|
: await collect(io.stdin);
|
|
60
76
|
const name = safeName(input.member ?? "stdin");
|
|
61
77
|
if (name === undefined) throw new FsError("member must not be empty.", "invalid");
|
|
62
|
-
await ctx.permission.assert([{ target: archive, boundary: archive.full }]);
|
|
78
|
+
await ctx.permission.assert([{ target: archive, boundary: outputBoundary(archive.full) }]);
|
|
63
79
|
const writer = new ZipWriter();
|
|
64
80
|
writer.addBuffer(data, name.split(path.sep).join("/"), {
|
|
65
81
|
mtime: new Date(),
|
|
@@ -69,31 +85,35 @@ export const zipTool: ToolSpec<ZipInput> = {
|
|
|
69
85
|
});
|
|
70
86
|
writer.end();
|
|
71
87
|
const streamed = await collect(writer.outputStream as AsyncIterable<Buffer>);
|
|
72
|
-
|
|
73
|
-
await fs.writeFile(archive.full, streamed).catch((error) => {
|
|
74
|
-
throw fromErrno(error);
|
|
75
|
-
});
|
|
88
|
+
await writeOutput(archive.full, streamed, false, 0o666, ctx.signal);
|
|
76
89
|
return textResult("", { archive: input.archive, members: 1 });
|
|
77
90
|
}
|
|
78
91
|
|
|
79
92
|
const members: Array<{ name: string; full: string; stats: Awaited<ReturnType<typeof fs.lstat>> }> = [];
|
|
80
93
|
for (const requested of requestedPaths) {
|
|
94
|
+
throwIfAborted(ctx.signal);
|
|
81
95
|
const target = await ctx.sandbox.resolveExisting(requested, { allowRoot: true });
|
|
82
96
|
const stats = await ctx.sandbox.lstat(target.full, requested);
|
|
83
97
|
const base = path.basename(path.resolve(requested));
|
|
84
98
|
members.push({ name: base, full: target.full, stats });
|
|
85
|
-
if (stats.isDirectory())
|
|
99
|
+
if (stats.isDirectory()) {
|
|
100
|
+
await collectMembers(target.full, base, members, ctx.concurrency, ctx.signal);
|
|
101
|
+
}
|
|
86
102
|
}
|
|
87
|
-
const entries = await
|
|
88
|
-
members
|
|
103
|
+
const entries = await mapLimit(
|
|
104
|
+
members,
|
|
105
|
+
ctx.concurrency,
|
|
106
|
+
async (member) => ({
|
|
89
107
|
target: await ctx.sandbox.resolveExisting(member.full, { allowRoot: true }),
|
|
90
108
|
boundary: member.stats.isDirectory() ? member.full : path.dirname(member.full),
|
|
91
|
-
})
|
|
109
|
+
}),
|
|
110
|
+
ctx.signal,
|
|
92
111
|
);
|
|
93
|
-
await ctx.permission.assert([...entries, { target: archive, boundary: archive.full }]);
|
|
112
|
+
await ctx.permission.assert([...entries, { target: archive, boundary: outputBoundary(archive.full) }]);
|
|
94
113
|
|
|
95
114
|
const writer = new ZipWriter();
|
|
96
115
|
for (const member of members) {
|
|
116
|
+
throwIfAborted(ctx.signal);
|
|
97
117
|
const name = member.name.split(path.sep).join("/");
|
|
98
118
|
if (member.stats.isDirectory()) {
|
|
99
119
|
/* v8 ignore next */
|
|
@@ -118,10 +138,7 @@ export const zipTool: ToolSpec<ZipInput> = {
|
|
|
118
138
|
}
|
|
119
139
|
writer.end();
|
|
120
140
|
const data = await collect(writer.outputStream as AsyncIterable<Buffer>);
|
|
121
|
-
|
|
122
|
-
await fs.writeFile(archive.full, data).catch((error) => {
|
|
123
|
-
throw fromErrno(error);
|
|
124
|
-
});
|
|
141
|
+
await writeOutput(archive.full, data, false, 0o666, ctx.signal);
|
|
125
142
|
return textResult("", { archive: input.archive, members: members.length });
|
|
126
143
|
},
|
|
127
144
|
};
|
|
@@ -133,8 +150,8 @@ export const unzipTool: ToolSpec<UnzipInput> = {
|
|
|
133
150
|
pipeable: false,
|
|
134
151
|
mutates: true,
|
|
135
152
|
async run(ctx, input) {
|
|
136
|
-
const archive = await ctx.sandbox.
|
|
137
|
-
await ctx.permission.assert([{ target: archive, boundary: archive.full }]);
|
|
153
|
+
const archive = await ctx.sandbox.resolveFollow(input.archive);
|
|
154
|
+
await ctx.permission.assert([{ target: archive, boundary: outputBoundary(archive.full) }]);
|
|
138
155
|
/* v8 ignore next */
|
|
139
156
|
const buffer = await fs.readFile(archive.full).catch((error) => {
|
|
140
157
|
throw fromErrno(error);
|
|
@@ -142,44 +159,90 @@ export const unzipTool: ToolSpec<UnzipInput> = {
|
|
|
142
159
|
const zip = await openZip(buffer);
|
|
143
160
|
const names: string[] = [];
|
|
144
161
|
const rootTarget = input.directory === undefined
|
|
145
|
-
? await ctx.sandbox
|
|
146
|
-
: await ctx.sandbox
|
|
162
|
+
? await resolveOutputTarget(ctx.sandbox, ".")
|
|
163
|
+
: await resolveOutputTarget(ctx.sandbox, input.directory);
|
|
147
164
|
const root = rootTarget.full;
|
|
148
165
|
if (input.mode === "list") {
|
|
149
|
-
for (const entry of zip.entries)
|
|
166
|
+
for (const entry of zip.entries) {
|
|
167
|
+
throwIfAborted(ctx.signal);
|
|
168
|
+
names.push(entry.fileName);
|
|
169
|
+
}
|
|
150
170
|
return textResult(names.map((name) => `${name}\n`).join(""), { archive: input.archive, members: names });
|
|
151
171
|
}
|
|
152
|
-
|
|
153
|
-
|
|
172
|
+
const planned: Array<{
|
|
173
|
+
entry: yauzl.Entry;
|
|
174
|
+
name: string;
|
|
175
|
+
kind: "file" | "directory" | "symlink";
|
|
176
|
+
target: Awaited<ReturnType<typeof resolveArchiveTarget>>;
|
|
177
|
+
linkname: string | undefined;
|
|
178
|
+
mode: number;
|
|
179
|
+
}> = [];
|
|
180
|
+
const plannedPaths = new Set<string>();
|
|
154
181
|
for (const entry of zip.entries) {
|
|
182
|
+
throwIfAborted(ctx.signal);
|
|
155
183
|
const name = safeName(entry.fileName);
|
|
156
184
|
/* v8 ignore next */
|
|
157
185
|
if (name === undefined) continue;
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
186
|
+
const unixMode = entry.externalFileAttributes >>> 16;
|
|
187
|
+
const hasUnixMode = unixMode !== 0;
|
|
188
|
+
const kind = entry.fileName.endsWith("/")
|
|
189
|
+
? "directory"
|
|
190
|
+
: hasUnixMode && (unixMode & 0o170000) === 0o120000
|
|
191
|
+
? "symlink"
|
|
192
|
+
: "file";
|
|
193
|
+
const mode = hasUnixMode ? unixMode & 0o7777 : kind === "directory" ? 0o755 : 0o644;
|
|
194
|
+
const target = await resolveArchiveTarget(
|
|
195
|
+
ctx.sandbox,
|
|
196
|
+
root,
|
|
197
|
+
path.join(root, name),
|
|
198
|
+
kind,
|
|
199
|
+
);
|
|
200
|
+
if (kind !== "directory") {
|
|
201
|
+
if (input.noOverwrite === true && (plannedPaths.has(target.full) || (await exists(target.full)))) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (input.overwrite !== true && (await exists(target.full))) {
|
|
205
|
+
throw new FsError(`File exists: "${target.full}"`, "exists");
|
|
206
|
+
}
|
|
164
207
|
}
|
|
165
|
-
|
|
166
|
-
if (
|
|
167
|
-
|
|
208
|
+
let linkname: string | undefined;
|
|
209
|
+
if (kind === "symlink") {
|
|
210
|
+
linkname = (await collect(await openEntry(zip.file, entry))).toString("utf8");
|
|
211
|
+
assertSafeLinkTarget(linkname);
|
|
168
212
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
213
|
+
plannedPaths.add(target.full);
|
|
214
|
+
planned.push({ entry, name, kind, target, linkname, mode });
|
|
215
|
+
}
|
|
216
|
+
throwIfAborted(ctx.signal);
|
|
217
|
+
assertArchiveSymlinkPlan(planned);
|
|
218
|
+
await assertArchivePlan(ctx.permission, planned, { target: rootTarget, boundary: root });
|
|
219
|
+
await fs.mkdir(root, { recursive: true });
|
|
220
|
+
for (const item of planned) {
|
|
221
|
+
throwIfAborted(ctx.signal);
|
|
222
|
+
if (item.kind === "directory") {
|
|
223
|
+
await fs.mkdir(item.target.full, { recursive: true });
|
|
224
|
+
} else if (item.kind === "symlink") {
|
|
225
|
+
await fs.mkdir(path.dirname(item.target.full), { recursive: true });
|
|
226
|
+
await fs.symlink(item.linkname as string, item.target.full);
|
|
179
227
|
} else {
|
|
180
|
-
|
|
228
|
+
const data = await collect(await openEntry(zip.file, item.entry));
|
|
229
|
+
await fs.mkdir(path.dirname(item.target.full), { recursive: true });
|
|
230
|
+
const mode = safeMode(item.mode);
|
|
231
|
+
await writeOutput(
|
|
232
|
+
item.target.full,
|
|
233
|
+
data,
|
|
234
|
+
false,
|
|
235
|
+
mode,
|
|
236
|
+
ctx.signal,
|
|
237
|
+
mode,
|
|
238
|
+
);
|
|
181
239
|
}
|
|
182
|
-
names.push(name);
|
|
240
|
+
names.push(item.name);
|
|
241
|
+
}
|
|
242
|
+
const directories = finalDirectoryPlans(planned);
|
|
243
|
+
for (const item of directories) {
|
|
244
|
+
throwIfAborted(ctx.signal);
|
|
245
|
+
await chmodDirectoryNoFollow(item.target.full, safeDirectoryMode(item.mode));
|
|
183
246
|
}
|
|
184
247
|
zip.file.close();
|
|
185
248
|
return textResult("", { archive: input.archive, members: names });
|
|
@@ -190,14 +253,19 @@ async function collectMembers(
|
|
|
190
253
|
full: string,
|
|
191
254
|
name: string,
|
|
192
255
|
members: Array<{ name: string; full: string; stats: Awaited<ReturnType<typeof fs.lstat>> }>,
|
|
256
|
+
concurrency: number,
|
|
257
|
+
signal: AbortSignal,
|
|
193
258
|
): Promise<void> {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
259
|
+
throwIfAborted(signal);
|
|
260
|
+
const walked = await walkAll(full, {
|
|
261
|
+
recursive: true,
|
|
262
|
+
includeHidden: true,
|
|
263
|
+
concurrency,
|
|
264
|
+
signal,
|
|
265
|
+
});
|
|
266
|
+
for (const entry of walked) {
|
|
267
|
+
const relative = path.relative(full, entry.path).split(path.sep).join("/");
|
|
268
|
+
members.push({ name: `${name}/${relative}`, full: entry.path, stats: entry.stats });
|
|
201
269
|
}
|
|
202
270
|
}
|
|
203
271
|
|
|
@@ -247,10 +315,7 @@ function safeName(name: string): string | undefined {
|
|
|
247
315
|
return parts.length === 0 ? undefined : parts.join(path.sep);
|
|
248
316
|
}
|
|
249
317
|
|
|
250
|
-
|
|
251
|
-
const rel = path.relative(root, target);
|
|
252
|
-
return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
|
|
253
|
-
}
|
|
318
|
+
|
|
254
319
|
|
|
255
320
|
async function exists(target: string): Promise<boolean> {
|
|
256
321
|
try {
|
package/src/core/spill.ts
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* SPDX-License-Identifier: EPL-2.0
|
|
3
|
-
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { randomUUID } from "node:crypto";
|
|
7
|
-
import * as fs from "node:fs/promises";
|
|
8
|
-
import * as os from "node:os";
|
|
9
|
-
import type { FileHandle } from "node:fs/promises";
|
|
10
|
-
import * as path from "node:path";
|
|
11
|
-
import { FsError } from "./errors.js";
|
|
12
|
-
import type { ToolResult } from "./types.js";
|
|
13
|
-
|
|
14
|
-
/** The dynamic threshold never goes below this. */
|
|
15
|
-
export const MIN_SPILL_THRESHOLD = 16 * 1024 * 1024;
|
|
16
|
-
|
|
17
|
-
/** The dynamic threshold never goes above this. */
|
|
18
|
-
export const MAX_SPILL_THRESHOLD = 512 * 1024 * 1024;
|
|
19
|
-
|
|
20
|
-
/** Bytes of a spilled result returned to the host as a preview. */
|
|
21
|
-
export const SPILL_PREVIEW = 64 * 1024;
|
|
22
|
-
|
|
23
|
-
/** A quarter of free memory, clamped to a safe range. */
|
|
24
|
-
export function dynamicSpillThreshold(free = os.freemem()): number {
|
|
25
|
-
return Math.max(MIN_SPILL_THRESHOLD, Math.min(MAX_SPILL_THRESHOLD, Math.floor(free / 4)));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** An explicit byte count from the plugin options; undefined means dynamic. */
|
|
29
|
-
export function normalizeSpillThreshold(value: unknown): number | undefined {
|
|
30
|
-
if (value === undefined) return undefined;
|
|
31
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
32
|
-
throw new FsError("spillThresholdBytes must be a positive number.", "invalid");
|
|
33
|
-
}
|
|
34
|
-
return Math.trunc(value);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** The shared directory for spilled outputs under the plugin's temp area. */
|
|
38
|
-
export function spillDirectory(tmpdir: string): string {
|
|
39
|
-
return path.join(tmpdir, "opencode", "fs-spill");
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface SpillResult {
|
|
43
|
-
/** The full output when it stayed in memory, else its first bytes. */
|
|
44
|
-
readonly preview: Buffer;
|
|
45
|
-
/** Set when the full output was written to disk. */
|
|
46
|
-
readonly path: string | undefined;
|
|
47
|
-
/** Total bytes written. */
|
|
48
|
-
readonly bytes: number;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Collects chunks with a bounded in-memory prefix. Once a direct call passes
|
|
53
|
-
* `threshold`, the full stream goes to a temporary file and `finish` returns
|
|
54
|
-
* only the prefix; pipe stages pass `memoryOnly` and keep the whole buffer.
|
|
55
|
-
*/
|
|
56
|
-
export class SpillSink {
|
|
57
|
-
private chunks: Buffer[] = [];
|
|
58
|
-
private head: Buffer[] = [];
|
|
59
|
-
private headBytes = 0;
|
|
60
|
-
private bytes = 0;
|
|
61
|
-
private handle: FileHandle | undefined;
|
|
62
|
-
private filePath: string | undefined;
|
|
63
|
-
|
|
64
|
-
constructor(
|
|
65
|
-
private readonly dir: string,
|
|
66
|
-
private readonly name: string,
|
|
67
|
-
private readonly memoryOnly = false,
|
|
68
|
-
private readonly threshold = dynamicSpillThreshold(),
|
|
69
|
-
private readonly previewBytes = SPILL_PREVIEW,
|
|
70
|
-
) {}
|
|
71
|
-
|
|
72
|
-
async write(chunk: Buffer): Promise<void> {
|
|
73
|
-
if (chunk.length === 0) return;
|
|
74
|
-
this.bytes += chunk.length;
|
|
75
|
-
if (this.headBytes < this.previewBytes) {
|
|
76
|
-
const take = Math.min(chunk.length, this.previewBytes - this.headBytes);
|
|
77
|
-
this.head.push(chunk.subarray(0, take));
|
|
78
|
-
this.headBytes += take;
|
|
79
|
-
}
|
|
80
|
-
if (this.handle !== undefined) {
|
|
81
|
-
await this.handle.write(chunk);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
this.chunks.push(chunk);
|
|
85
|
-
if (!this.memoryOnly && this.bytes > this.threshold) await this.spill();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
private async spill(): Promise<void> {
|
|
89
|
-
await fs.mkdir(this.dir, { recursive: true });
|
|
90
|
-
this.filePath = path.join(this.dir, `${this.name}-${randomUUID()}.out`);
|
|
91
|
-
this.handle = await fs.open(this.filePath, "w");
|
|
92
|
-
for (const chunk of this.chunks) await this.handle.write(chunk);
|
|
93
|
-
this.chunks = [];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async finish(): Promise<SpillResult> {
|
|
97
|
-
const preview = this.handle === undefined ? Buffer.concat(this.chunks) : Buffer.concat(this.head);
|
|
98
|
-
const filePath = this.filePath;
|
|
99
|
-
if (this.handle !== undefined) {
|
|
100
|
-
await this.handle.close();
|
|
101
|
-
this.handle = undefined;
|
|
102
|
-
}
|
|
103
|
-
return { preview, path: filePath, bytes: this.bytes };
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Closes and removes a partial spill file. */
|
|
107
|
-
async abort(): Promise<void> {
|
|
108
|
-
if (this.handle !== undefined) {
|
|
109
|
-
/* v8 ignore next */
|
|
110
|
-
await this.handle.close().catch(() => undefined);
|
|
111
|
-
this.handle = undefined;
|
|
112
|
-
}
|
|
113
|
-
if (this.filePath !== undefined) {
|
|
114
|
-
/* v8 ignore next */
|
|
115
|
-
await fs.rm(this.filePath, { force: true }).catch(() => undefined);
|
|
116
|
-
this.filePath = undefined;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Spill a finished tool result that is larger than the threshold. Tools that
|
|
123
|
-
* already streamed into a sink carry `outputPath` and pass through.
|
|
124
|
-
*/
|
|
125
|
-
export async function spillResult(
|
|
126
|
-
result: ToolResult,
|
|
127
|
-
name: string,
|
|
128
|
-
tmpdir: string,
|
|
129
|
-
threshold = dynamicSpillThreshold(),
|
|
130
|
-
): Promise<ToolResult> {
|
|
131
|
-
if (result.stdout.length <= threshold || result.metadata?.outputPath !== undefined) return result;
|
|
132
|
-
const sink = new SpillSink(spillDirectory(tmpdir), name, false, 0);
|
|
133
|
-
try {
|
|
134
|
-
await sink.write(result.stdout);
|
|
135
|
-
const out = await sink.finish();
|
|
136
|
-
return {
|
|
137
|
-
...result,
|
|
138
|
-
stdout: out.preview,
|
|
139
|
-
metadata: { ...result.metadata, bytes: out.bytes, outputPath: out.path, spilled: true },
|
|
140
|
-
};
|
|
141
|
-
} catch (error) {
|
|
142
|
-
await sink.abort();
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
}
|