feat: bootstrap agent workspace and AGENTS.md
This commit is contained in:
32
src/agents/workspace.test.ts
Normal file
32
src/agents/workspace.test.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ensureAgentWorkspace } from "./workspace.js";
|
||||
|
||||
describe("ensureAgentWorkspace", () => {
|
||||
it("creates directory and AGENTS.md when missing", async () => {
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-ws-"));
|
||||
const nested = path.join(dir, "nested");
|
||||
const result = await ensureAgentWorkspace({
|
||||
dir: nested,
|
||||
ensureAgentsFile: true,
|
||||
});
|
||||
expect(result.dir).toBe(path.resolve(nested));
|
||||
expect(result.agentsPath).toBe(
|
||||
path.join(path.resolve(nested), "AGENTS.md"),
|
||||
);
|
||||
expect(result.agentsPath).toBeDefined();
|
||||
if (!result.agentsPath) throw new Error("agentsPath missing");
|
||||
const content = await fs.readFile(result.agentsPath, "utf-8");
|
||||
expect(content).toContain("# AGENTS.md");
|
||||
});
|
||||
|
||||
it("does not overwrite existing AGENTS.md", async () => {
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-ws-"));
|
||||
const agentsPath = path.join(dir, "AGENTS.md");
|
||||
await fs.writeFile(agentsPath, "custom", "utf-8");
|
||||
await ensureAgentWorkspace({ dir, ensureAgentsFile: true });
|
||||
expect(await fs.readFile(agentsPath, "utf-8")).toBe("custom");
|
||||
});
|
||||
});
|
||||
46
src/agents/workspace.ts
Normal file
46
src/agents/workspace.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
||||
|
||||
export const DEFAULT_AGENT_WORKSPACE_DIR = path.join(CONFIG_DIR, "workspace");
|
||||
export const DEFAULT_AGENTS_FILENAME = "AGENTS.md";
|
||||
|
||||
const DEFAULT_AGENTS_TEMPLATE = `# AGENTS.md — Clawdis Workspace
|
||||
|
||||
This folder is the assistant’s working directory.
|
||||
|
||||
## Safety defaults
|
||||
- Don’t exfiltrate secrets or private data.
|
||||
- Don’t run destructive commands unless explicitly asked.
|
||||
- Be concise in chat; write longer output to files in this workspace.
|
||||
|
||||
## How to use this
|
||||
- Put project notes, scratch files, and “memory” here.
|
||||
- Customize this file with additional instructions for your assistant.
|
||||
`;
|
||||
|
||||
export async function ensureAgentWorkspace(params?: {
|
||||
dir?: string;
|
||||
ensureAgentsFile?: boolean;
|
||||
}): Promise<{ dir: string; agentsPath?: string }> {
|
||||
const rawDir = params?.dir?.trim()
|
||||
? params.dir.trim()
|
||||
: DEFAULT_AGENT_WORKSPACE_DIR;
|
||||
const dir = resolveUserPath(rawDir);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
|
||||
if (!params?.ensureAgentsFile) return { dir };
|
||||
|
||||
const agentsPath = path.join(dir, DEFAULT_AGENTS_FILENAME);
|
||||
try {
|
||||
await fs.writeFile(agentsPath, DEFAULT_AGENTS_TEMPLATE, {
|
||||
encoding: "utf-8",
|
||||
flag: "wx",
|
||||
});
|
||||
} catch (err) {
|
||||
const anyErr = err as { code?: string };
|
||||
if (anyErr.code !== "EEXIST") throw err;
|
||||
}
|
||||
return { dir, agentsPath };
|
||||
}
|
||||
Reference in New Issue
Block a user