UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

87 lines (86 loc) • 3.27 kB
import { t as asBoolean } from "./boolean-2vRuVafy.mjs"; import { t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as textResult } from "./tool-results-CcV0sxnb.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import { l as readMediaBuffer } from "./store-zsTAVy24.mjs"; import "./media-store-2NPKQiFG.mjs"; import { a as FILE_TRANSFER_SUBDIR, o as FILE_WRITE_HARD_MAX_BYTES, s as FILE_WRITE_TOOL_DESCRIPTOR } from "./descriptors-CZtmxLD2.mjs"; import { n as normalizeBase64ForCompare, t as inspectStrictBase64 } from "./base64-DBAnfGFU.mjs"; import { n as readRequiredNodePath, r as humanSize, t as invokeNodeToolPayload } from "./node-tool-invoke-DIC-uNGI.mjs"; import crypto from "node:crypto"; //#region extensions/file-transfer/src/tools/file-write-tool.ts function decodeStrictBase64(e) { const t = inspectStrictBase64(e); if (t === void 0) throw new Error("contentBase64 is not valid base64"); if (t > 16777216) throw new Error(`decoded content is ${t} bytes; maximum is ${FILE_WRITE_HARD_MAX_BYTES} bytes (${humanSize(FILE_WRITE_HARD_MAX_BYTES)})`); const n = Buffer.from(e, "base64"); if (normalizeBase64ForCompare(n.toString("base64")) !== normalizeBase64ForCompare(e)) throw new Error("contentBase64 is not valid base64"); return n; } async function readSourceBytes(e) { const t = e.sourceMediaId?.trim(); if (t) { const { buffer: e } = await readMediaBuffer(t, FILE_TRANSFER_SUBDIR, FILE_WRITE_HARD_MAX_BYTES); return { buffer: e, contentBase64: e.toString("base64"), source: "media" }; } if (e.contentBase64 === void 0) throw new Error("contentBase64 or sourceMediaId required"); return { buffer: decodeStrictBase64(e.contentBase64), contentBase64: e.contentBase64, source: "inline" }; } function createFileWriteTool() { return { ...FILE_WRITE_TOOL_DESCRIPTOR, async execute(e, t) { const n = asNonArrayRecord(t); const { node: r, requestedPath: i } = readRequiredNodePath(n); const a = typeof n.contentBase64 === "string" ? n.contentBase64 : void 0; const o = typeof n.sourceMediaId === "string" ? n.sourceMediaId : void 0; const s = asBoolean(n.overwrite) ?? false; const c = asBoolean(n.createParents) ?? false; const l = await readSourceBytes({ contentBase64: a, sourceMediaId: o }); const u = l.buffer; const d = crypto.createHash("sha256").update(u).digest("hex"); const { audit: f, payload: p } = await invokeNodeToolPayload({ node: r, params: n, command: "file.write", commandParams: { path: i, contentBase64: l.contentBase64, overwrite: s, createParents: c, expectedSha256: d }, invalidPayloadMessage: "unexpected response from node", invalidPayloadError: "unexpected file.write response from node", errorAuditExtra: { sizeBytes: u.byteLength }, requireOk: true, requestedPath: i }); const m = p; await f({ canonicalPath: m.path, decision: "allowed", sizeBytes: m.size, sha256: m.sha256 }); const h = m.overwritten ? " (overwrote existing file)" : ""; return textResult(`Wrote ${m.path} (${humanSize(m.size)}, sha256:${m.sha256.slice(0, 12)})${h}`, { ...m, source: l.source }); } }; } //#endregion export { createFileWriteTool };