openclaw-multi-session-plugins/dist/src/exportArtifacts.js
2026-05-06 09:33:54 +08:00

489 lines
17 KiB
JavaScript

import { createHash } from "node:crypto";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
const DEFAULT_MAX_FILES = 64;
const DEFAULT_MAX_INLINE_BYTES = 10 * 1024 * 1024;
const SKIPPED_DIRS = new Set([
".git",
".openclaw",
".xworkmate",
".pi",
".dart_tool",
".next",
".turbo",
"build",
"dist",
"node_modules",
]);
export async function prepareXWorkmateArtifacts(input) {
const params = input.params ?? {};
const pluginConfig = input.pluginConfig ?? {};
const runId = requiredString(params.runId, "runId required");
const sessionKey = requiredString(params.sessionKey, "sessionKey required");
const workspaceDir = resolveWorkspaceDir({
config: input.config,
pluginConfig,
params,
sessionKey,
});
const workspaceRoot = await fs.realpath(workspaceDir);
const artifactScope = artifactScopeFor(sessionKey, runId);
const scopeRoot = resolveScopeRoot(workspaceRoot, artifactScope);
await fs.mkdir(scopeRoot, { recursive: true });
return {
runId,
sessionKey,
remoteWorkingDirectory: workspaceRoot,
remoteWorkspaceRefKind: "remotePath",
artifactScope,
scopeKind: "task",
artifactDirectory: scopeRoot,
relativeArtifactDirectory: artifactScope,
warnings: [],
};
}
export async function exportXWorkmateArtifacts(input) {
const params = input.params ?? {};
const pluginConfig = input.pluginConfig ?? {};
const runId = requiredString(params.runId, "runId required");
const sessionKey = requiredString(params.sessionKey, "sessionKey required");
const maxFiles = positiveInteger(params.maxFiles, pluginConfig.maxFiles, DEFAULT_MAX_FILES);
const maxInlineBytes = nonNegativeInteger(params.maxInlineBytes, pluginConfig.maxInlineBytes, DEFAULT_MAX_INLINE_BYTES);
const sinceUnixMs = nonNegativeNumber(params.sinceUnixMs, 0);
const includeContent = optionalBoolean(params.includeContent, true);
const latestIfEmpty = optionalBoolean(params.latestIfEmpty, false);
const workspaceDir = resolveWorkspaceDir({
config: input.config,
pluginConfig,
params,
sessionKey,
});
const workspaceRoot = await fs.realpath(workspaceDir);
const warnings = [];
const artifactScope = optionalArtifactScope(params.artifactScope);
const scopeRoot = artifactScope ? resolveScopeRoot(workspaceRoot, artifactScope) : workspaceRoot;
const scopedExport = artifactScope !== "";
let scopeKind = scopedExport ? "task" : "workspace";
let candidates = await collectCandidates({
scanRoot: scopeRoot,
relativeRoot: scopeRoot,
sinceUnixMs,
warnings,
});
if (candidates.length === 0 && latestIfEmpty) {
const latestWarnings = [];
const latestCandidates = await collectCandidates({
scanRoot: workspaceRoot,
relativeRoot: workspaceRoot,
sinceUnixMs: 0,
warnings: latestWarnings,
});
if (latestCandidates.length > 0) {
warnings.push(...latestWarnings);
if (scopedExport) {
warnings.push("scoped artifact directory is empty; exported latest workspace files instead");
}
candidates = latestCandidates;
scopeKind = "workspace-latest";
}
}
candidates.sort((left, right) => {
if (right.mtimeMs !== left.mtimeMs) {
return right.mtimeMs - left.mtimeMs;
}
return left.relativePath.localeCompare(right.relativePath);
});
const artifacts = [];
for (const candidate of candidates) {
if (artifacts.length >= maxFiles) {
warnings.push(`artifact limit reached; skipped remaining files after ${maxFiles}`);
break;
}
const bytes = await fs.readFile(candidate.absolutePath);
const artifact = {
relativePath: candidate.relativePath,
label: path.posix.basename(candidate.relativePath),
contentType: contentTypeForPath(candidate.relativePath),
sizeBytes: bytes.byteLength,
sha256: createHash("sha256").update(bytes).digest("hex"),
scopeKind,
};
if (scopeKind === "task" && artifactScope) {
artifact.artifactScope = artifactScope;
}
if (includeContent && bytes.byteLength <= maxInlineBytes) {
artifact.encoding = "base64";
artifact.content = bytes.toString("base64");
}
else if (includeContent) {
warnings.push(`${candidate.relativePath} exceeds maxInlineBytes and was not inlined`);
}
artifacts.push(artifact);
}
const result = {
runId,
sessionKey,
remoteWorkingDirectory: workspaceRoot,
remoteWorkspaceRefKind: "remotePath",
...(scopeKind === "task" && artifactScope ? { artifactScope } : {}),
scopeKind,
artifacts,
warnings,
};
return {
...result,
manifestMarkdown: formatArtifactManifestMarkdown(result),
};
}
export async function readXWorkmateArtifact(input) {
const params = input.params ?? {};
const pluginConfig = input.pluginConfig ?? {};
const runId = optionalString(params.runId) || "read";
const sessionKey = requiredString(params.sessionKey, "sessionKey required");
const relativePath = safeInputRelativePath(params.relativePath, "relativePath");
const artifactScope = optionalArtifactScope(params.artifactScope);
const maxInlineBytes = nonNegativeInteger(params.maxInlineBytes, pluginConfig.maxInlineBytes, DEFAULT_MAX_INLINE_BYTES);
const workspaceDir = resolveWorkspaceDir({
config: input.config,
pluginConfig,
params,
sessionKey,
});
const workspaceRoot = await fs.realpath(workspaceDir);
const scopeRoot = artifactScope ? resolveScopeRoot(workspaceRoot, artifactScope) : workspaceRoot;
const scopeKind = artifactScope ? "task" : "workspace";
const absolutePath = path.join(scopeRoot, relativePath.split("/").join(path.sep));
const realPath = await fs.realpath(absolutePath);
if (!isWithinRoot(scopeRoot, realPath)) {
throw new Error("relativePath must stay inside the workspace");
}
const stat = await fs.stat(realPath);
if (!stat.isFile()) {
throw new Error("relativePath must point to a file");
}
const bytes = await fs.readFile(realPath);
const artifact = {
relativePath: safeRelativePath(scopeRoot, realPath),
label: path.posix.basename(relativePath),
contentType: contentTypeForPath(relativePath),
sizeBytes: bytes.byteLength,
sha256: createHash("sha256").update(bytes).digest("hex"),
scopeKind,
};
if (artifactScope) {
artifact.artifactScope = artifactScope;
}
const warnings = [];
if (bytes.byteLength <= maxInlineBytes) {
artifact.encoding = "base64";
artifact.content = bytes.toString("base64");
}
else {
warnings.push(`${artifact.relativePath} exceeds maxInlineBytes and was not inlined`);
}
const result = {
runId,
sessionKey,
remoteWorkingDirectory: workspaceRoot,
remoteWorkspaceRefKind: "remotePath",
...(artifactScope ? { artifactScope } : {}),
scopeKind,
artifacts: [artifact],
warnings,
};
return {
...result,
manifestMarkdown: formatArtifactManifestMarkdown(result),
};
}
export function formatArtifactManifestMarkdown(input) {
const lines = [
"## XWorkmate artifacts",
"",
`Workspace: \`${input.remoteWorkingDirectory}\``,
input.artifactScope ? `Artifact scope: \`${input.artifactScope}\`` : `Artifact scope: \`${input.scopeKind ?? "workspace"}\``,
"",
];
if (input.artifacts.length === 0) {
lines.push("No artifacts found.");
}
else {
lines.push("| File | Type | Size | SHA-256 | Inline |");
lines.push("| --- | --- | ---: | --- | --- |");
for (const artifact of input.artifacts) {
lines.push(`| \`${escapeMarkdownCell(artifact.relativePath)}\` | ${escapeMarkdownCell(artifact.contentType)} | ${formatBytes(artifact.sizeBytes)} | \`${artifact.sha256.slice(0, 12)}\` | ${artifact.encoding === "base64" ? "yes" : "no"} |`);
}
}
if (input.warnings.length > 0) {
lines.push("", "Warnings:");
for (const warning of input.warnings) {
lines.push(`- ${warning}`);
}
}
return lines.join("\n");
}
async function collectCandidates(input) {
const candidates = [];
await walk(input.scanRoot);
return candidates;
async function walk(currentDir) {
let entries;
try {
entries = await fs.readdir(currentDir, { withFileTypes: true });
}
catch (error) {
input.warnings.push(`cannot read ${safeDisplayPath(input.relativeRoot, currentDir)}: ${String(error)}`);
return;
}
entries.sort((left, right) => left.name.localeCompare(right.name));
for (const entry of entries) {
if (entry.name === "." || entry.name === "..") {
continue;
}
const absolutePath = path.join(currentDir, entry.name);
if (entry.isSymbolicLink()) {
input.warnings.push(`skipped symlink ${safeDisplayPath(input.relativeRoot, absolutePath)}`);
continue;
}
if (entry.isDirectory()) {
if (SKIPPED_DIRS.has(entry.name)) {
continue;
}
await walk(absolutePath);
continue;
}
if (!entry.isFile()) {
continue;
}
const stat = await fs.stat(absolutePath);
const changedAtMs = Math.max(stat.mtimeMs, stat.ctimeMs);
if (changedAtMs < input.sinceUnixMs) {
continue;
}
const realPath = await fs.realpath(absolutePath);
if (!isWithinRoot(input.relativeRoot, realPath)) {
input.warnings.push(`skipped path outside workspace ${entry.name}`);
continue;
}
const relativePath = safeRelativePath(input.relativeRoot, realPath);
if (!relativePath) {
continue;
}
candidates.push({
absolutePath: realPath,
relativePath,
sizeBytes: stat.size,
mtimeMs: changedAtMs,
});
}
}
}
function artifactScopeFor(sessionKey, runId) {
return [
".xworkmate",
"artifacts",
"tasks",
safeScopeSegment(sessionKey),
safeScopeSegment(runId),
].join("/");
}
function safeScopeSegment(value) {
const normalized = value
.trim()
.replaceAll(path.sep, "_")
.replace(/[^A-Za-z0-9._-]+/g, "_")
.replace(/^[._-]+|[._-]+$/g, "")
.slice(0, 48);
const digest = createHash("sha256").update(value).digest("hex").slice(0, 12);
return `${normalized || "scope"}-${digest}`;
}
function optionalArtifactScope(value) {
const scope = optionalString(value);
if (!scope) {
return "";
}
return safeInputRelativePath(scope, "artifactScope");
}
function safeInputRelativePath(value, label) {
const relativePath = optionalString(value);
if (!relativePath) {
throw new Error(`${label} required`);
}
if (path.isAbsolute(relativePath) || relativePath.includes("\0")) {
throw new Error(`${label} must stay inside the workspace`);
}
const normalized = relativePath.split(/[\\/]/).filter(Boolean).join("/");
if (!normalized || normalized.split("/").some((part) => part === ".." || part === ".")) {
throw new Error(`${label} must stay inside the workspace`);
}
return normalized;
}
function resolveScopeRoot(workspaceRoot, artifactScope) {
const normalizedScope = safeInputRelativePath(artifactScope, "artifactScope");
const scopeRoot = path.join(workspaceRoot, normalizedScope.split("/").join(path.sep));
if (!isWithinRoot(workspaceRoot, scopeRoot)) {
throw new Error("artifactScope must stay inside the workspace");
}
return scopeRoot;
}
function resolveWorkspaceDir(input) {
const explicit = optionalString(input.params.workspaceDir) || optionalString(input.pluginConfig.workspaceDir);
if (explicit) {
return expandUserPath(explicit);
}
const config = objectRecord(input.config);
const agents = objectRecord(config.agents);
const agentList = Array.isArray(agents.list)
? agents.list.map(objectRecord).filter((entry) => Object.keys(entry).length > 0)
: [];
const agentId = agentIdFromSessionKey(input.sessionKey);
const selected = (agentId ? agentList.find((entry) => optionalString(entry.id) === agentId) : undefined) ??
agentList.find((entry) => entry.default === true) ??
agentList[0];
const selectedWorkspace = selected ? optionalString(selected.workspace) : "";
if (selectedWorkspace) {
return expandUserPath(selectedWorkspace);
}
const defaults = objectRecord(agents.defaults);
const defaultWorkspace = optionalString(defaults.workspace);
if (defaultWorkspace) {
return expandUserPath(defaultWorkspace);
}
const profile = process.env.OPENCLAW_PROFILE?.trim();
if (profile && profile.toLowerCase() !== "default") {
return path.join(os.homedir(), ".openclaw", `workspace-${profile}`);
}
return path.join(os.homedir(), ".openclaw", "workspace");
}
function agentIdFromSessionKey(sessionKey) {
const parts = sessionKey.split(":");
if (parts.length >= 3 && parts[0] === "agent") {
return parts[1]?.trim() ?? "";
}
return "";
}
function safeRelativePath(root, target) {
const relative = path.relative(root, target);
if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
return "";
}
const normalized = relative.split(path.sep).join(path.posix.sep);
if (normalized.split("/").some((part) => part === ".." || part === "")) {
return "";
}
return normalized;
}
function safeDisplayPath(root, target) {
return safeRelativePath(root, target) || path.basename(target);
}
function isWithinRoot(root, target) {
const relative = path.relative(root, target);
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
}
function contentTypeForPath(relativePath) {
switch (path.extname(relativePath).toLowerCase()) {
case ".md":
case ".markdown":
return "text/markdown";
case ".txt":
case ".log":
return "text/plain";
case ".json":
return "application/json";
case ".csv":
return "text/csv";
case ".html":
case ".htm":
return "text/html";
case ".pdf":
return "application/pdf";
case ".pptx":
return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
case ".docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case ".xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
case ".png":
return "image/png";
case ".jpg":
case ".jpeg":
return "image/jpeg";
case ".gif":
return "image/gif";
case ".svg":
return "image/svg+xml";
default:
return "application/octet-stream";
}
}
function objectRecord(value) {
return value && typeof value === "object" && !Array.isArray(value)
? value
: {};
}
function optionalString(value) {
return typeof value === "string" ? value.trim() : "";
}
function requiredString(value, message) {
const resolved = optionalString(value);
if (!resolved) {
throw new Error(message);
}
return resolved;
}
function optionalBoolean(value, fallback) {
if (typeof value === "boolean") {
return value;
}
return fallback;
}
function positiveInteger(primary, secondary, fallback) {
for (const value of [primary, secondary]) {
const numeric = Number(value);
if (Number.isFinite(numeric) && numeric > 0) {
return Math.floor(numeric);
}
}
return fallback;
}
function nonNegativeInteger(primary, secondary, fallback) {
for (const value of [primary, secondary]) {
const numeric = Number(value);
if (Number.isFinite(numeric) && numeric >= 0) {
return Math.floor(numeric);
}
}
return fallback;
}
function nonNegativeNumber(value, fallback) {
const numeric = Number(value);
if (Number.isFinite(numeric) && numeric >= 0) {
return numeric;
}
return fallback;
}
function expandUserPath(value) {
if (value === "~") {
return os.homedir();
}
if (value.startsWith("~/")) {
return path.join(os.homedir(), value.slice(2));
}
return path.resolve(value);
}
function formatBytes(sizeBytes) {
if (sizeBytes < 1024) {
return `${sizeBytes} B`;
}
const kib = sizeBytes / 1024;
if (kib < 1024) {
return `${Math.round(kib)} KB`;
}
const mib = kib / 1024;
return `${mib.toFixed(mib >= 10 ? 0 : 1)} MB`;
}
function escapeMarkdownCell(value) {
return value.replaceAll("|", "\\|");
}