Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
import { readFile, rm } from "node:fs/promises";
import { join } from "node:path";
import { isFileExists, writeFileAtomic } from "./fs-utils";
export const managedAgentsHeader = [
"<!-- Webstudio-managed project agent instructions. -->",
"<!-- Source: Project Settings > Agents. Do not edit this file directly. -->",
].join("\n");
export type ManagedAgentsResult =
| "created"
| "updated"
| "removed"
| "unchanged"
| "blocked-by-user-file";
export type ManagedAgentsDependencies = {
isFileExists: typeof isFileExists;
readFile: typeof readFile;
removeFile: typeof rm;
writeFileAtomic: typeof writeFileAtomic;
};
const defaultDependencies: ManagedAgentsDependencies = {
isFileExists,
readFile,
removeFile: rm,
writeFileAtomic,
};
const createManagedContent = (instructions: string) =>
`${managedAgentsHeader}\n\n${instructions.trim()}\n`;
export const materializeManagedAgents = async ({
rootDir,
instructions,
dependencies = defaultDependencies,
}: {
rootDir: string;
instructions: string | undefined;
dependencies?: ManagedAgentsDependencies;
}): Promise<{ path: string; status: ManagedAgentsResult }> => {
const path = join(rootDir, "AGENTS.md");
const exists = await dependencies.isFileExists(path);
const current = exists
? await dependencies.readFile(path, "utf8")
: undefined;
const isManaged = current?.startsWith(managedAgentsHeader) === true;
const normalizedInstructions = instructions?.trim() ?? "";
if (normalizedInstructions === "") {
if (isManaged) {
await dependencies.removeFile(path, { force: true });
return { path, status: "removed" };
}
return { path, status: "unchanged" };
}
if (current !== undefined && isManaged === false) {
return { path, status: "blocked-by-user-file" };
}
const next = createManagedContent(normalizedInstructions);
if (current === next) {
return { path, status: "unchanged" };
}
await dependencies.writeFileAtomic(path, next);
return { path, status: current === undefined ? "created" : "updated" };
};