feat: one-click DSH import into real agent configs (instructions + skill wiring)
Browse files- ext/dsh-plugin/client.js +26 -6
- ext/dsh-plugin/index.js +25 -1
- src/cli/commands/import.ts +34 -0
- src/cli/index.ts +12 -0
- src/import/dsh-import.ts +132 -0
- tests/import.test.ts +35 -0
ext/dsh-plugin/client.js
CHANGED
|
@@ -69,6 +69,18 @@ window.__ModuleLoader__.load({
|
|
| 69 |
return String(d.text || '');
|
| 70 |
}
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
async function callExport(out) {
|
| 73 |
const r = await fetch('/agentlens/export', {
|
| 74 |
method: 'POST',
|
|
@@ -181,6 +193,7 @@ window.__ModuleLoader__.load({
|
|
| 181 |
const [path, setPath] = useState(rememberedPath());
|
| 182 |
const [workspaces, setWorkspaces] = useState([]);
|
| 183 |
const [exportOut, setExportOut] = useState('');
|
|
|
|
| 184 |
|
| 185 |
useEffect(() => {
|
| 186 |
let alive = true;
|
|
@@ -313,10 +326,17 @@ window.__ModuleLoader__.load({
|
|
| 313 |
h(
|
| 314 |
'div',
|
| 315 |
{ className: 'row' },
|
| 316 |
-
h('span', { style: { opacity: 0.75 } }, '完整搬家:'),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
h('input', {
|
| 318 |
type: 'text',
|
| 319 |
-
placeholder: '
|
| 320 |
value: exportOut,
|
| 321 |
onChange: (e) => setExportOut(e.target.value),
|
| 322 |
}),
|
|
@@ -325,12 +345,12 @@ window.__ModuleLoader__.load({
|
|
| 325 |
{
|
| 326 |
type: 'button',
|
| 327 |
disabled: busy !== '',
|
| 328 |
-
title: '
|
| 329 |
onClick: async () => {
|
| 330 |
-
setBusy('
|
| 331 |
setError('');
|
| 332 |
try {
|
| 333 |
-
setOut(await
|
| 334 |
} catch (err) {
|
| 335 |
setError(String((err && err.message) || err));
|
| 336 |
} finally {
|
|
@@ -338,7 +358,7 @@ window.__ModuleLoader__.load({
|
|
| 338 |
}
|
| 339 |
},
|
| 340 |
},
|
| 341 |
-
busy === '
|
| 342 |
)
|
| 343 |
),
|
| 344 |
error ? h('p', { className: 'agentlens-hint', style: { color: '#ff8a80' } }, `出错了:${error}`) : null,
|
|
|
|
| 69 |
return String(d.text || '');
|
| 70 |
}
|
| 71 |
|
| 72 |
+
async function callImport(target, out) {
|
| 73 |
+
const r = await fetch('/agentlens/import', {
|
| 74 |
+
method: 'POST',
|
| 75 |
+
credentials: 'same-origin',
|
| 76 |
+
headers: { 'content-type': 'application/json' },
|
| 77 |
+
body: JSON.stringify({ target, out }),
|
| 78 |
+
});
|
| 79 |
+
const d = await r.json();
|
| 80 |
+
if (!r.ok || d.ok === false) throw new Error(d.error || `HTTP ${r.status}`);
|
| 81 |
+
return String(d.text || '');
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
async function callExport(out) {
|
| 85 |
const r = await fetch('/agentlens/export', {
|
| 86 |
method: 'POST',
|
|
|
|
| 193 |
const [path, setPath] = useState(rememberedPath());
|
| 194 |
const [workspaces, setWorkspaces] = useState([]);
|
| 195 |
const [exportOut, setExportOut] = useState('');
|
| 196 |
+
const [importTarget, setImportTarget] = useState('codex');
|
| 197 |
|
| 198 |
useEffect(() => {
|
| 199 |
let alive = true;
|
|
|
|
| 326 |
h(
|
| 327 |
'div',
|
| 328 |
{ className: 'row' },
|
| 329 |
+
h('span', { style: { opacity: 0.75 } }, '完整搬家到:'),
|
| 330 |
+
h(
|
| 331 |
+
'select',
|
| 332 |
+
{ value: importTarget, onChange: (e) => setImportTarget(e.target.value) },
|
| 333 |
+
[['codex', 'Codex'], ['claude', 'Claude Code'], ['zcode', 'ZCode'], ['workbuddy', 'WorkBuddy'], ['mimo', 'MiMo Desktop']].map(
|
| 334 |
+
([value, label]) => h('option', { key: value, value }, label)
|
| 335 |
+
)
|
| 336 |
+
),
|
| 337 |
h('input', {
|
| 338 |
type: 'text',
|
| 339 |
+
placeholder: '资料目录(留空 = ~/<目标>-import)',
|
| 340 |
value: exportOut,
|
| 341 |
onChange: (e) => setExportOut(e.target.value),
|
| 342 |
}),
|
|
|
|
| 345 |
{
|
| 346 |
type: 'button',
|
| 347 |
disabled: busy !== '',
|
| 348 |
+
title: '把 DSH 全部资料(人物设定 + 文件资料 + 对话记录)一键导入目标 Agent 并写进它的全局配置',
|
| 349 |
onClick: async () => {
|
| 350 |
+
setBusy('import');
|
| 351 |
setError('');
|
| 352 |
try {
|
| 353 |
+
setOut(await callImport(importTarget, exportOut.trim()));
|
| 354 |
} catch (err) {
|
| 355 |
setError(String((err && err.message) || err));
|
| 356 |
} finally {
|
|
|
|
| 358 |
}
|
| 359 |
},
|
| 360 |
},
|
| 361 |
+
busy === 'import' ? '导入中…' : '一键导入'
|
| 362 |
)
|
| 363 |
),
|
| 364 |
error ? h('p', { className: 'agentlens-hint', style: { color: '#ff8a80' } }, `出错了:${error}`) : null,
|
ext/dsh-plugin/index.js
CHANGED
|
@@ -149,6 +149,30 @@ function mountRoutes(server, entry, fallbackCwd) {
|
|
| 149 |
},
|
| 150 |
};
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
const exportRoute = {
|
| 153 |
kind: "exact",
|
| 154 |
path: "/agentlens/export",
|
|
@@ -197,7 +221,7 @@ function mountRoutes(server, entry, fallbackCwd) {
|
|
| 197 |
};
|
| 198 |
|
| 199 |
const disposers = [];
|
| 200 |
-
for (const route of [scanRoute, migrateRoute, installRoute, workspacesRoute, exportRoute]) {
|
| 201 |
try {
|
| 202 |
disposers.push(server.register(route));
|
| 203 |
} catch (error) {
|
|
|
|
| 149 |
},
|
| 150 |
};
|
| 151 |
|
| 152 |
+
const importRoute = {
|
| 153 |
+
kind: "exact",
|
| 154 |
+
path: "/agentlens/import",
|
| 155 |
+
async handler(req, res) {
|
| 156 |
+
try {
|
| 157 |
+
const body = await new Promise((resolve) => {
|
| 158 |
+
let raw = "";
|
| 159 |
+
req.on("data", (chunk) => {
|
| 160 |
+
raw += chunk;
|
| 161 |
+
});
|
| 162 |
+
req.on("end", () => resolve(raw));
|
| 163 |
+
});
|
| 164 |
+
const payload = body ? JSON.parse(body) : {};
|
| 165 |
+
const target = String(payload.target || "codex");
|
| 166 |
+
const args = ["import", "--from", "dsh", "--to", target];
|
| 167 |
+
if (payload.out) args.push("--out", String(payload.out));
|
| 168 |
+
const text = await runAgentlens(entry, args, process.cwd());
|
| 169 |
+
json(res, 200, { ok: true, text });
|
| 170 |
+
} catch (error) {
|
| 171 |
+
json(res, 500, { ok: false, error: String((error && error.message) || error) });
|
| 172 |
+
}
|
| 173 |
+
},
|
| 174 |
+
};
|
| 175 |
+
|
| 176 |
const exportRoute = {
|
| 177 |
kind: "exact",
|
| 178 |
path: "/agentlens/export",
|
|
|
|
| 221 |
};
|
| 222 |
|
| 223 |
const disposers = [];
|
| 224 |
+
for (const route of [scanRoute, migrateRoute, installRoute, workspacesRoute, exportRoute, importRoute]) {
|
| 225 |
try {
|
| 226 |
disposers.push(server.register(route));
|
| 227 |
} catch (error) {
|
src/cli/commands/import.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { importDsh, type ImportTarget } from "../../import/dsh-import.js";
|
| 2 |
+
|
| 3 |
+
interface ImportOpts {
|
| 4 |
+
from: string;
|
| 5 |
+
to: string;
|
| 6 |
+
out?: string;
|
| 7 |
+
withAttachments?: boolean;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
export async function importData(opts: ImportOpts): Promise<void> {
|
| 11 |
+
if (opts.from !== "dsh") {
|
| 12 |
+
console.log(`暂不支持从 "${opts.from}" 导入。当前支持:dsh`);
|
| 13 |
+
return;
|
| 14 |
+
}
|
| 15 |
+
const valid: ImportTarget[] = ["codex", "claude", "zcode", "workbuddy", "mimo"];
|
| 16 |
+
if (!valid.includes(opts.to as ImportTarget)) {
|
| 17 |
+
console.log(`不支持的目标 "${opts.to}"。可选:${valid.join(", ")}`);
|
| 18 |
+
return;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
console.log(`正在把 DSH 全部资料导入 ${opts.to}(人物设定 + 文件资料 + 对话记录)...\n`);
|
| 22 |
+
|
| 23 |
+
const r = importDsh({
|
| 24 |
+
target: opts.to as ImportTarget,
|
| 25 |
+
dataDir: opts.out,
|
| 26 |
+
withAttachments: opts.withAttachments === true,
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
console.log(` ✅ 数据目录:${r.dataDir}`);
|
| 30 |
+
console.log(` ✅ 全局指令:${r.instructionsFile}(已写入导入段落,幂等)`);
|
| 31 |
+
if (r.skillFile) console.log(` ✅ Skill:${r.skillFile}`);
|
| 32 |
+
console.log(`\n 人物设定 ${r.personas} 个 · 对话 ${r.conversations} 条 · 资料 ${(r.materialsBytes / 1024 / 1024).toFixed(1)} MB`);
|
| 33 |
+
console.log(`\n重启 ${opts.to} 后,它会自动读取这些资料。`);
|
| 34 |
+
}
|
src/cli/index.ts
CHANGED
|
@@ -76,6 +76,18 @@ program
|
|
| 76 |
await exportData(opts);
|
| 77 |
});
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
program
|
| 80 |
.command("mcp")
|
| 81 |
.description("Start AgentLens as an MCP server (for Claude Code, Cursor, etc.)")
|
|
|
|
| 76 |
await exportData(opts);
|
| 77 |
});
|
| 78 |
|
| 79 |
+
program
|
| 80 |
+
.command("import")
|
| 81 |
+
.description("One-click: export DSH data AND wire it into a target agent's real config (instructions + skill)")
|
| 82 |
+
.requiredOption("--from <agent>", "source agent (dsh)")
|
| 83 |
+
.requiredOption("--to <agent>", "target agent (codex|claude|zcode|workbuddy|mimo)")
|
| 84 |
+
.option("-o, --out <dir>", "data directory (default ~/<target>-import)")
|
| 85 |
+
.option("--with-attachments", "also copy attachments (can be hundreds of MB)")
|
| 86 |
+
.action(async (opts) => {
|
| 87 |
+
const { importData } = await import("./commands/import.js");
|
| 88 |
+
await importData(opts);
|
| 89 |
+
});
|
| 90 |
+
|
| 91 |
program
|
| 92 |
.command("mcp")
|
| 93 |
.description("Start AgentLens as an MCP server (for Claude Code, Cursor, etc.)")
|
src/import/dsh-import.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
| 2 |
+
import { homedir } from "node:os";
|
| 3 |
+
import { dirname, join } from "node:path";
|
| 4 |
+
import { exportDsh } from "../export/dsh.js";
|
| 5 |
+
|
| 6 |
+
export type ImportTarget = "codex" | "claude" | "zcode" | "workbuddy" | "mimo";
|
| 7 |
+
|
| 8 |
+
interface TargetSpec {
|
| 9 |
+
label: string;
|
| 10 |
+
instructionsFile: string;
|
| 11 |
+
skillsDir?: string;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
const HOME = homedir();
|
| 15 |
+
|
| 16 |
+
const TARGETS: Record<ImportTarget, TargetSpec> = {
|
| 17 |
+
codex: { label: "Codex", instructionsFile: join(HOME, ".codex", "AGENTS.md"), skillsDir: join(HOME, ".codex", "skills") },
|
| 18 |
+
claude: { label: "Claude Code", instructionsFile: join(HOME, ".claude", "CLAUDE.md"), skillsDir: join(HOME, ".claude", "skills") },
|
| 19 |
+
zcode: { label: "ZCode", instructionsFile: join(HOME, ".zcode", "AGENTS.md"), skillsDir: join(HOME, ".zcode", "skills") },
|
| 20 |
+
workbuddy: { label: "WorkBuddy", instructionsFile: join(HOME, ".workbuddy", "AGENTS.md"), skillsDir: join(HOME, ".workbuddy", "skills") },
|
| 21 |
+
mimo: { label: "MiMo Desktop", instructionsFile: join(HOME, ".agents", "AGENTS.md"), skillsDir: join(HOME, ".agents", "skills") },
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
const MARK_START = "<!-- agentlens-import:start -->";
|
| 25 |
+
const MARK_END = "<!-- agentlens-import:end -->";
|
| 26 |
+
|
| 27 |
+
export interface DshImportOptions {
|
| 28 |
+
target: ImportTarget;
|
| 29 |
+
dataDir?: string;
|
| 30 |
+
withAttachments?: boolean;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
export interface DshImportResult {
|
| 34 |
+
target: ImportTarget;
|
| 35 |
+
dataDir: string;
|
| 36 |
+
instructionsFile: string;
|
| 37 |
+
skillFile?: string;
|
| 38 |
+
personas: number;
|
| 39 |
+
conversations: number;
|
| 40 |
+
materialsBytes: number;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function sectionFor(dataDir: string): string {
|
| 44 |
+
return [
|
| 45 |
+
MARK_START,
|
| 46 |
+
"## DSH 导入资料(AgentLens)",
|
| 47 |
+
"",
|
| 48 |
+
"以下资料从 DeepSeek Harness 一键导入,回答前先阅读后再作答:",
|
| 49 |
+
"",
|
| 50 |
+
`- 人物设定:\`${join(dataDir, "personas.md")}\``,
|
| 51 |
+
`- 工作区文件:\`${join(dataDir, "materials", "workspace")}\``,
|
| 52 |
+
`- 记忆快照:\`${join(dataDir, "materials", "memory")}\``,
|
| 53 |
+
`- 对话记录:\`${join(dataDir, "conversations")}\``,
|
| 54 |
+
"",
|
| 55 |
+
"使用规则:涉及用户偏好/角色设定时先读 personas.md;需要资料时查 materials/;需要历史上下文时检索 conversations/。",
|
| 56 |
+
MARK_END,
|
| 57 |
+
].join("\n");
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
export function mergeInstructions(file: string, dataDir: string): void {
|
| 61 |
+
let existing = "";
|
| 62 |
+
if (existsSync(file)) existing = readFileSync(file, "utf8");
|
| 63 |
+
const section = sectionFor(dataDir);
|
| 64 |
+
const startIdx = existing.indexOf(MARK_START);
|
| 65 |
+
const endIdx = existing.indexOf(MARK_END);
|
| 66 |
+
let merged: string;
|
| 67 |
+
if (startIdx !== -1 && endIdx !== -1) {
|
| 68 |
+
merged = existing.slice(0, startIdx) + section + existing.slice(endIdx + MARK_END.length);
|
| 69 |
+
} else {
|
| 70 |
+
merged = existing.trimEnd() + "\n\n" + section + "\n";
|
| 71 |
+
}
|
| 72 |
+
mkdirSync(dirname(file), { recursive: true });
|
| 73 |
+
writeFileSync(file, merged, "utf8");
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
function writeSkill(skillsDir: string | undefined, dataDir: string): string | undefined {
|
| 77 |
+
if (!skillsDir) return undefined;
|
| 78 |
+
const dir = join(skillsDir, "dsh-import");
|
| 79 |
+
mkdirSync(dir, { recursive: true });
|
| 80 |
+
const file = join(dir, "SKILL.md");
|
| 81 |
+
const content = [
|
| 82 |
+
"---",
|
| 83 |
+
"name: dsh-import",
|
| 84 |
+
"description: DSH 导入资料(人物设定、工作区文件、历史对话)。当用户提到你的角色设定、以前做过什么、之前的工作文件、或需要参考历史对话时使用。",
|
| 85 |
+
"---",
|
| 86 |
+
"",
|
| 87 |
+
"# DSH 导入资料",
|
| 88 |
+
"",
|
| 89 |
+
`从 DeepSeek Harness 导入的完整资料,位于 \`${dataDir}\`:`,
|
| 90 |
+
"",
|
| 91 |
+
"| 内容 | 路径 | 什么时候用 |",
|
| 92 |
+
"|---|---|---|",
|
| 93 |
+
`| 人物设定 | \`${join(dataDir, "personas.md")}\` | 用户问"你是谁""按我的设定来"时 |`,
|
| 94 |
+
`| 工作区文件 | \`${join(dataDir, "materials", "workspace")}\` | 需要之前的文件资料时 |`,
|
| 95 |
+
`| 记忆快照 | \`${join(dataDir, "materials", "memory")}\` | 需要历史决策/结论时 |`,
|
| 96 |
+
`| 对话记录 | \`${join(dataDir, "conversations")}\` | 需要之前聊过什么时 |`,
|
| 97 |
+
"",
|
| 98 |
+
"阅读顺序:先 personas.md 了解角色设定,再按需检索 materials / conversations。",
|
| 99 |
+
"",
|
| 100 |
+
].join("\n");
|
| 101 |
+
writeFileSync(file, content, "utf8");
|
| 102 |
+
return file;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
export function importDsh(options: DshImportOptions): DshImportResult {
|
| 106 |
+
const spec = TARGETS[options.target];
|
| 107 |
+
if (!spec) throw new Error(`不支持的目标: ${options.target}`);
|
| 108 |
+
|
| 109 |
+
const dataDir = options.dataDir && options.dataDir.length > 0
|
| 110 |
+
? options.dataDir
|
| 111 |
+
: join(HOME, `${options.target}-import`);
|
| 112 |
+
|
| 113 |
+
const result = exportDsh({
|
| 114 |
+
outDir: dataDir,
|
| 115 |
+
withAttachments: options.withAttachments === true,
|
| 116 |
+
withMaterials: true,
|
| 117 |
+
withConversations: true,
|
| 118 |
+
});
|
| 119 |
+
|
| 120 |
+
mergeInstructions(spec.instructionsFile, dataDir);
|
| 121 |
+
const skillFile = writeSkill(spec.skillsDir, dataDir);
|
| 122 |
+
|
| 123 |
+
return {
|
| 124 |
+
target: options.target,
|
| 125 |
+
dataDir,
|
| 126 |
+
instructionsFile: spec.instructionsFile,
|
| 127 |
+
skillFile,
|
| 128 |
+
personas: result.personas,
|
| 129 |
+
conversations: result.conversationLines,
|
| 130 |
+
materialsBytes: result.materialsBytes,
|
| 131 |
+
};
|
| 132 |
+
}
|
tests/import.test.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, it, expect } from "vitest";
|
| 2 |
+
import { mergeInstructions } from "../src/import/dsh-import.js";
|
| 3 |
+
import { readFileSync, writeFileSync, mkdtempSync } from "node:fs";
|
| 4 |
+
import { join } from "node:path";
|
| 5 |
+
import { tmpdir } from "node:os";
|
| 6 |
+
|
| 7 |
+
function tempFile(): string {
|
| 8 |
+
const dir = mkdtempSync(join(tmpdir(), "agentlens-import-"));
|
| 9 |
+
return join(dir, "AGENTS.md");
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
describe("mergeInstructions", () => {
|
| 13 |
+
it("appends the import section and keeps existing content", () => {
|
| 14 |
+
const file = tempFile();
|
| 15 |
+
writeFileSync(file, "# Existing rules\n\n- Keep this\n", "utf8");
|
| 16 |
+
mergeInstructions(file, "/data/dsh");
|
| 17 |
+
const out = readFileSync(file, "utf8");
|
| 18 |
+
expect(out).toContain("- Keep this");
|
| 19 |
+
expect(out).toContain("agentlens-import:start");
|
| 20 |
+
expect(out).toContain("/data/dsh/personas.md");
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
it("is idempotent across repeated runs", () => {
|
| 24 |
+
const file = tempFile();
|
| 25 |
+
writeFileSync(file, "# Existing\n", "utf8");
|
| 26 |
+
mergeInstructions(file, "/data/a");
|
| 27 |
+
mergeInstructions(file, "/data/a");
|
| 28 |
+
mergeInstructions(file, "/data/b");
|
| 29 |
+
const out = readFileSync(file, "utf8");
|
| 30 |
+
// Exactly one section, and it reflects the latest data dir.
|
| 31 |
+
expect((out.match(/agentlens-import:start/g) || []).length).toBe(1);
|
| 32 |
+
expect(out).toContain("/data/b/personas.md");
|
| 33 |
+
expect(out).not.toContain("/data/a/personas.md");
|
| 34 |
+
});
|
| 35 |
+
});
|