@sema-agent/core
Version:
Stateless, task-oriented AI agent core
94 lines (93 loc) • 3.81 kB
JavaScript
import { mkdirSync, readFileSync, writeFileSync, unlinkSync } from "node:fs";
import { join, resolve, sep } from "node:path";
import { callerText } from "../core/caller-text.js";
import { physicalPathOfExisting } from "../core/physical-path.js";
import { sanitizeScope, scopeLabelOf } from "../stores/file/fs-atomic.js";
function isPlainObject(v) {
return typeof v === "object" && v !== null && !Array.isArray(v);
}
export function mergeWorkflowArgs(callArgs, defaultArgs) {
const cloneDefaults = (v) => {
try {
return structuredClone(v);
}
catch {
return v;
}
};
if (callArgs === undefined)
return cloneDefaults(defaultArgs);
if (defaultArgs === undefined)
return callArgs;
if (isPlainObject(callArgs) && isPlainObject(defaultArgs)) {
return { ...cloneDefaults(defaultArgs), ...callArgs };
}
return callArgs;
}
export function normalizeStringArg(callArgs, defaultArgs, key = "topic") {
if (typeof callArgs === "string" && isPlainObject(defaultArgs))
return { [key]: callArgs };
return callArgs;
}
function safeFileStem(id) {
if (typeof id !== "string")
return undefined;
return /^[A-Za-z0-9._-]{1,120}$/.test(id) && !id.startsWith(".") ? id : undefined;
}
function containedBy(root, p) {
return p === root || p.startsWith(root + sep);
}
export function createFileWorkflowScriptStore(dir) {
const root = resolve(dir);
const scopeDir = (scope) => join(root, sanitizeScope(scope));
return {
scopePartitioned: true,
persist(runId, script, scope) {
const stem = safeFileStem(runId);
if (stem === undefined)
throw new Error(`workflow script store: unsafe runId for a filename: ${typeof runId === "string" ? JSON.stringify(runId) : callerText(runId)}`);
const base = scopeDir(scope);
mkdirSync(base, { recursive: true, mode: 0o700 });
const path = join(base, `${stem}.js`);
writeFileSync(path, script, { encoding: "utf-8", mode: 0o600 });
return path;
},
load(scriptPath, scope) {
const base = scopeDir(scope);
const full = resolve(scriptPath);
if (!containedBy(base, full)) {
throw new Error(`workflow script store: scriptPath is outside this scope's script partition`);
}
const physRoot = physicalPathOfExisting(root);
const physBase = physicalPathOfExisting(base);
const physFull = physicalPathOfExisting(full);
const refusePhysical = () => {
throw new Error(`workflow script store: scriptPath is outside this scope's script partition after symlink resolution`);
};
if (!containedBy(physRoot, physBase))
refusePhysical();
if (!containedBy(physBase, physFull))
refusePhysical();
return readFileSync(physFull, "utf-8");
},
deleteRun(runId, scope) {
const stem = safeFileStem(runId);
if (stem === undefined)
return false;
const base = join(root, scopeLabelOf(scope));
const physBase = physicalPathOfExisting(base);
if (!containedBy(physicalPathOfExisting(root), physBase)) {
throw new Error(`workflow script store: this scope's script partition resolves outside the store — refusing to delete through it`);
}
try {
unlinkSync(join(physBase, `${stem}.js`));
return true;
}
catch (err) {
if (err.code === "ENOENT")
return false;
throw err;
}
},
};
}