openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
136 lines (135 loc) • 4.95 kB
JavaScript
import { f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { Lc as validateAgentsWorkspaceGetParams, Rc as validateAgentsWorkspaceListParams } from "./src-BX8OAe1J.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { n as detectMime } from "./mime-D2wVBzpa.mjs";
import { n as defineValidatedGatewayHandler } from "./validation-DwNk6A-B.mjs";
import { n as WORKSPACE_PREVIEW_MAX_BYTES } from "./workspace-file-limits-oDqtf1PU.mjs";
import { a as normalizeRelativePath, d as sortWorkspaceEntries, f as statWorkspacePath, i as listWorkspacePath, l as resolveWorkspacePath, m as toWorkspaceBrowserEntry, n as decodeUtf8Strict, p as toUpdatedAtMs, s as readWorkspaceFile } from "./workspace-fs-e2DnPT_x.mjs";
import { n as resolveConfiguredAgentIdOrRespondError } from "./agent-id-shared-YyMfd879.mjs";
import { i as sessionFilesError } from "./session-files-errors-4OQSCO6W.mjs";
import path from "node:path";
//#region src/gateway/server-methods/agents-workspace.ts
const MAX_IMAGE_BYTES = 5242880;
const DEFAULT_LIST_LIMIT = 250;
const MAX_LIST_LIMIT = 500;
const IMAGE_EXTENSIONS = /* @__PURE__ */ new Set([
".avif",
".bmp",
".gif",
".heic",
".heif",
".jpeg",
".jpg",
".png",
".webp"
]);
const SUPPORTED_IMAGE_MIME_TYPES = /* @__PURE__ */ new Set([
"image/avif",
"image/bmp",
"image/gif",
"image/heic",
"image/heif",
"image/jpeg",
"image/png",
"image/webp"
]);
function resolveWorkspaceScopeOrRespond(e, t, n) {
const r = resolveConfiguredAgentIdOrRespondError(e.agentId, t, n);
if (!r) return null;
const i = resolveAgentWorkspaceDir(t, r);
const a = e.path ?? "";
const o = a.replaceAll("\\", "/");
if (path.posix.isAbsolute(o) || path.win32.isAbsolute(a)) {
n(false, void 0, sessionFilesError("workspace_path_invalid", "path must be workspace-relative", { path: a }));
return null;
}
const s = normalizeRelativePath(e.path);
if (!resolveWorkspacePath(i, s || ".")) {
n(false, void 0, sessionFilesError("workspace_path_invalid", "path escapes the agent workspace", { path: e.path ?? "" }));
return null;
}
return {
agentId: r,
workspaceDir: i,
browserPath: s
};
}
const agentsWorkspaceHandlers = {
"agents.workspace.list": defineValidatedGatewayHandler("agents.workspace.list", validateAgentsWorkspaceListParams, async ({ params: e, respond: t, context: n }) => {
const r = resolveWorkspaceScopeOrRespond(e, n.getRuntimeConfig(), t);
if (!r) return;
const { agentId: i, workspaceDir: a, browserPath: o } = r;
const s = (await statWorkspacePath(a, o))?.isDirectory ? await listWorkspacePath(a, o) : void 0;
if (!s) {
t(false, void 0, sessionFilesError("workspace_path_not_found", "workspace directory not found", { path: o }));
return;
}
const c = sortWorkspaceEntries(s.map((e) => toWorkspaceBrowserEntry(o ? `${o}/${e.name}` : e.name, e)).filter((e) => e !== void 0));
const l = Math.min(e.offset ?? 0, c.length);
const u = Math.min(e.limit ?? DEFAULT_LIST_LIMIT, MAX_LIST_LIMIT);
const d = path.dirname(o);
t(true, {
agentId: i,
path: o,
...o ? { parentPath: d === "." ? "" : d } : {},
entries: c.slice(l, l + u),
totalEntries: c.length,
offset: l
});
}),
"agents.workspace.get": defineValidatedGatewayHandler("agents.workspace.get", validateAgentsWorkspaceGetParams, async ({ params: e, respond: t, context: n }) => {
const r = resolveWorkspaceScopeOrRespond(e, n.getRuntimeConfig(), t);
if (!r) return;
const { agentId: i, workspaceDir: a, browserPath: o } = r;
const respondNotFound = () => {
t(false, void 0, sessionFilesError("workspace_file_not_found", "workspace file not found", { path: o }));
};
if (!o) {
respondNotFound();
return;
}
const s = await statWorkspacePath(a, o);
if (!s?.isFile) {
respondNotFound();
return;
}
const c = IMAGE_EXTENSIONS.has(path.extname(o).toLowerCase());
const l = c ? MAX_IMAGE_BYTES : WORKSPACE_PREVIEW_MAX_BYTES;
const u = s.size > l ? "too-large" : await readWorkspaceFile(a, o, { maxBytes: l });
if (u === "too-large") {
t(false, void 0, sessionFilesError("workspace_file_too_large", "workspace file is too large to preview", {
maxBytes: l,
path: o,
size: s.size
}));
return;
}
if (!u) {
respondNotFound();
return;
}
const d = {
path: o,
name: path.basename(o),
size: u.stat.size,
updatedAtMs: toUpdatedAtMs(u.stat.mtimeMs)
};
const f = c ? await detectMime({ buffer: u.buffer }) : "text/plain";
const p = c ? f && SUPPORTED_IMAGE_MIME_TYPES.has(f) ? u.buffer.toString("base64") : void 0 : decodeUtf8Strict(u.buffer);
if (p === void 0) {
t(false, void 0, sessionFilesError("workspace_file_unsupported", "workspace file is not UTF-8 text or a supported image", { path: o }));
return;
}
t(true, {
agentId: i,
file: {
...d,
mimeType: f,
encoding: c ? "base64" : "utf8",
content: p
}
});
})
};
//#endregion
export { agentsWorkspaceHandlers };