openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
139 lines (138 loc) • 4.95 kB
JavaScript
import { r as textResult } from "./tool-results-CcV0sxnb.mjs";
import { c as extractArchive, n as ArchiveLimitError, t as ARCHIVE_LIMIT_ERROR_CODE } from "./archive-xLn9WKtS.mjs";
import { d as saveMediaBuffer } from "./store-zsTAVy24.mjs";
import "./archive-CjZHeh-7.mjs";
import "./media-store-2NPKQiFG.mjs";
import { a as FILE_TRANSFER_SUBDIR, t as DIR_FETCH_TOOL_DESCRIPTOR } from "./descriptors-CZtmxLD2.mjs";
import { i as DIR_FETCH_HARD_MAX_BYTES, r as DIR_FETCH_DEFAULT_MAX_BYTES, t as DIR_FETCH_ARCHIVE_POLICY } from "./dir-fetch-archive-DwOataU3.mjs";
import { r as mimeFromExtension, t as IMAGE_MIME_INLINE_SET } from "./mime-Bqs6nBQk.mjs";
import { i as readClampedInt, n as readRequiredNodePath, t as invokeNodeToolPayload } from "./node-tool-invoke-DIC-uNGI.mjs";
import { t as renderDirectoryText } from "./directory-text-BSWL301l.mjs";
import { t as decodeFetchPayload } from "./fetch-payload-tpAVxkGH.mjs";
import path from "node:path";
import fs from "node:fs/promises";
import { walkDirectory } from "@openclaw/fs-safe/walk";
import { sha256File } from "@openclaw/fs-safe/durability";
//#region extensions/file-transfer/src/tools/dir-fetch-tool.ts
const MEDIA_URL_CAP = 25;
const TAR_UNPACK_TIMEOUT_MS = 6e4;
function createDirFetchTool() {
return {
...DIR_FETCH_TOOL_DESCRIPTOR,
execute: async (e, t) => {
const n = t;
const { node: r, requestedPath: i } = readRequiredNodePath(n);
const a = readClampedInt({
input: n,
key: "maxBytes",
defaultValue: DIR_FETCH_DEFAULT_MAX_BYTES,
hardMax: DIR_FETCH_HARD_MAX_BYTES
});
const { audit: o, payload: s } = await invokeNodeToolPayload({
node: r,
params: n,
command: "dir.fetch",
commandParams: {
path: i,
maxBytes: a
},
requestedPath: i
});
const { canonicalPath: c, size: l, sha256: u, buffer: d } = decodeFetchPayload("dir.fetch", s);
const f = await saveMediaBuffer(d, "application/gzip", FILE_TRANSFER_SUBDIR, DIR_FETCH_HARD_MAX_BYTES);
const p = path.dirname(f.path);
const m = `dir-fetch-${path.basename(f.path, path.extname(f.path))}`;
const h = path.join(p, m);
await fs.mkdir(h, {
recursive: true,
mode: 448
});
try {
await extractArchive({
archivePath: f.path,
destDir: h,
kind: "tar",
tarGzip: true,
timeoutMs: TAR_UNPACK_TIMEOUT_MS,
entryModes: "clamp",
...DIR_FETCH_ARCHIVE_POLICY
});
} catch (e) {
await Promise.all([fs.rm(h, {
recursive: true,
force: true
}).catch(() => void 0), fs.rm(f.path, { force: true }).catch(() => void 0)]);
const t = e instanceof Error ? e.message : String(e);
const n = e instanceof ArchiveLimitError && e.code !== ARCHIVE_LIMIT_ERROR_CODE.ENTRY_COUNT_EXCEEDS_LIMIT;
await o({
canonicalPath: c,
decision: "error",
errorCode: n ? "TREE_TOO_LARGE" : "UNSAFE_ARCHIVE",
errorMessage: t,
sizeBytes: l,
sha256: u
});
throw new Error(`dir.fetch ${n ? "UNCOMPRESSED_TOO_LARGE" : "UNSAFE_ARCHIVE"}: ${t}`, { cause: e });
}
const g = await walkDirectory(h, {
symlinks: "skip",
include: ({ kind: e }) => e === "file"
});
if (g.failedDirs.length > 0) throw g.failedDirs[0].error;
const _ = [];
for (const { relativePath: e, path: t } of g.entries) {
const { size: n } = await fs.stat(t);
const r = mimeFromExtension(e);
const i = (await sha256File(t)).digest;
_.push({
relPath: e,
size: n,
mimeType: r,
sha256: i,
localPath: t
});
}
const v = _.length;
const y = _.filter((e) => IMAGE_MIME_INLINE_SET.has(e.mimeType));
const b = _.filter((e) => !IMAGE_MIME_INLINE_SET.has(e.mimeType));
const x = [...y, ...b].slice(0, MEDIA_URL_CAP).map((e) => e.localPath);
await o({
canonicalPath: c,
decision: "allowed",
sizeBytes: l,
sha256: u
});
const S = JSON.stringify({
rootDir: h,
fileCount: _.length
}).slice(0, -1);
const C = renderDirectoryText({
entries: _.toSorted((e, t) => e.relPath < t.relPath ? -1 : e.relPath > t.relPath ? 1 : 0),
project: ({ relPath: e, size: t }) => ({
relPath: e,
size: t
}),
render: (e) => {
const t = `${S},"displayedCount":${e.length},"files":[${e.join(",")}]}`;
const n = `${_.length - e.length} saved files omitted from this text (byte limit or reserved path markers). All remain under rootDir; inspect them with available local file or directory capabilities.`;
return {
manifest: t,
text: `Fetched ${_.length} files.\n${t}\n${n}`
};
},
fallback: `Fetched ${_.length} files. Saved paths omitted: rootDir cannot be represented safely within the 8192-byte text limit. No usable local path is shown.`
});
return textResult(C, {
path: c,
rootDir: h,
fileCount: v,
tarBytes: l,
sha256: u,
files: _,
media: { mediaUrls: x }
});
}
};
}
//#endregion
export { createDirFetchTool };