feat: DSH bot-instruction parser + dedupe + DSH panel project picker
Browse files- ext/dsh-plugin/client.js +42 -2
- ext/dsh-plugin/index.js +46 -3
- src/cli/commands/drift.ts +1 -23
- src/cli/commands/move.ts +12 -28
- src/cli/commands/scan.ts +4 -25
- src/core/dedupe.ts +19 -0
- src/core/files.ts +43 -0
- src/mcp/server.ts +5 -25
- src/parsers/agents-md.ts +50 -1
- tests/parsers-extra.test.ts +23 -0
ext/dsh-plugin/client.js
CHANGED
|
@@ -69,6 +69,17 @@ window.__ModuleLoader__.load({
|
|
| 69 |
return String(d.text || '');
|
| 70 |
}
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
async function callInstall() {
|
| 73 |
const r = await fetch('/agentlens/install', { method: 'POST', credentials: 'same-origin' });
|
| 74 |
const d = await r.json();
|
|
@@ -156,6 +167,24 @@ window.__ModuleLoader__.load({
|
|
| 156 |
|
| 157 |
function MigratePanel() {
|
| 158 |
const [path, setPath] = useState(rememberedPath());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
const [from, setFrom] = useState('claude');
|
| 160 |
const [to, setTo] = useState('cursor');
|
| 161 |
const [busy, setBusy] = useState('');
|
|
@@ -199,14 +228,25 @@ window.__ModuleLoader__.load({
|
|
| 199 |
h(
|
| 200 |
'p',
|
| 201 |
{ className: 'agentlens-hint' },
|
| 202 |
-
'把
|
| 203 |
),
|
| 204 |
h(
|
| 205 |
'div',
|
| 206 |
{ className: 'row' },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
h('input', {
|
| 208 |
type: 'text',
|
| 209 |
-
placeholder: '项目路径
|
| 210 |
value: path,
|
| 211 |
onChange: (e) => savePath(e.target.value),
|
| 212 |
}),
|
|
|
|
| 69 |
return String(d.text || '');
|
| 70 |
}
|
| 71 |
|
| 72 |
+
async function fetchWorkspaces() {
|
| 73 |
+
try {
|
| 74 |
+
const r = await fetch('/agentlens/workspaces', { credentials: 'same-origin' });
|
| 75 |
+
const d = await r.json();
|
| 76 |
+
if (r.ok && d.ok !== false && Array.isArray(d.workspaces)) return d.workspaces;
|
| 77 |
+
} catch {
|
| 78 |
+
/* ignore */
|
| 79 |
+
}
|
| 80 |
+
return [];
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
async function callInstall() {
|
| 84 |
const r = await fetch('/agentlens/install', { method: 'POST', credentials: 'same-origin' });
|
| 85 |
const d = await r.json();
|
|
|
|
| 167 |
|
| 168 |
function MigratePanel() {
|
| 169 |
const [path, setPath] = useState(rememberedPath());
|
| 170 |
+
const [workspaces, setWorkspaces] = useState([]);
|
| 171 |
+
|
| 172 |
+
useEffect(() => {
|
| 173 |
+
let alive = true;
|
| 174 |
+
fetchWorkspaces().then((list) => {
|
| 175 |
+
if (!alive) return;
|
| 176 |
+
setWorkspaces(list);
|
| 177 |
+
setPath((current) => {
|
| 178 |
+
if (current) return current;
|
| 179 |
+
const first = list[0] || '';
|
| 180 |
+
if (first) rememberPath(first);
|
| 181 |
+
return first;
|
| 182 |
+
});
|
| 183 |
+
});
|
| 184 |
+
return () => {
|
| 185 |
+
alive = false;
|
| 186 |
+
};
|
| 187 |
+
}, []);
|
| 188 |
const [from, setFrom] = useState('claude');
|
| 189 |
const [to, setTo] = useState('cursor');
|
| 190 |
const [busy, setBusy] = useState('');
|
|
|
|
| 228 |
h(
|
| 229 |
'p',
|
| 230 |
{ className: 'agentlens-hint' },
|
| 231 |
+
'把项目里的规则文件(CLAUDE.md / AGENTS.md / .cursor/rules 等)在 9 个 Agent 之间一键迁移。先选择你的项目目录,再选源/目标 Agent。所有操作都在本机完成。'
|
| 232 |
),
|
| 233 |
h(
|
| 234 |
'div',
|
| 235 |
{ className: 'row' },
|
| 236 |
+
workspaces.length > 0
|
| 237 |
+
? h('select', {
|
| 238 |
+
value: workspaces.includes(path) ? path : '',
|
| 239 |
+
onChange: (e) => {
|
| 240 |
+
if (e.target.value) savePath(e.target.value);
|
| 241 |
+
},
|
| 242 |
+
title: '从 DSH 最近打开的项目中选择',
|
| 243 |
+
},
|
| 244 |
+
h('option', { value: '' }, '选择项目目录…'),
|
| 245 |
+
workspaces.map((w) => h('option', { key: w, value: w }, w)))
|
| 246 |
+
: null,
|
| 247 |
h('input', {
|
| 248 |
type: 'text',
|
| 249 |
+
placeholder: '或手动输入项目路径,例如 ~/你的项目',
|
| 250 |
value: path,
|
| 251 |
onChange: (e) => savePath(e.target.value),
|
| 252 |
}),
|
ext/dsh-plugin/index.js
CHANGED
|
@@ -12,13 +12,44 @@
|
|
| 12 |
*/
|
| 13 |
import { execFile } from "node:child_process";
|
| 14 |
import { promisify } from "node:util";
|
| 15 |
-
import { existsSync, accessSync, constants } from "node:fs";
|
| 16 |
|
| 17 |
const execFileAsync = promisify(execFile);
|
| 18 |
|
| 19 |
export const inject = ["tools", "webServer"];
|
| 20 |
|
| 21 |
const DEFAULT_ENTRY = '/Users/apple/agentlens/dist/cli/index.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Resolve a real Node.js binary. Inside the Electron host `process.execPath`
|
|
@@ -118,6 +149,18 @@ function mountRoutes(server, entry, fallbackCwd) {
|
|
| 118 |
},
|
| 119 |
};
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
const installRoute = {
|
| 122 |
kind: "exact",
|
| 123 |
path: "/agentlens/install",
|
|
@@ -132,7 +175,7 @@ function mountRoutes(server, entry, fallbackCwd) {
|
|
| 132 |
};
|
| 133 |
|
| 134 |
const disposers = [];
|
| 135 |
-
for (const route of [scanRoute, migrateRoute, installRoute]) {
|
| 136 |
try {
|
| 137 |
disposers.push(server.register(route));
|
| 138 |
} catch (error) {
|
|
@@ -148,7 +191,7 @@ function mountRoutes(server, entry, fallbackCwd) {
|
|
| 148 |
*/
|
| 149 |
export function apply(ctx, config = {}) {
|
| 150 |
const entry = resolveEntry(config);
|
| 151 |
-
const fallbackCwd =
|
| 152 |
|
| 153 |
// --- Local HTTP routes for the client UI ---
|
| 154 |
ctx.effect(() => {
|
|
|
|
| 12 |
*/
|
| 13 |
import { execFile } from "node:child_process";
|
| 14 |
import { promisify } from "node:util";
|
| 15 |
+
import { existsSync, accessSync, constants, readFileSync } from "node:fs";
|
| 16 |
|
| 17 |
const execFileAsync = promisify(execFile);
|
| 18 |
|
| 19 |
export const inject = ["tools", "webServer"];
|
| 20 |
|
| 21 |
const DEFAULT_ENTRY = '/Users/apple/agentlens/dist/cli/index.js';
|
| 22 |
+
const HOME = process.env.HOME || "/Users/apple";
|
| 23 |
+
|
| 24 |
+
/**
|
| 25 |
+
* Read recent workspace paths from DSH's own storage so the panel can default
|
| 26 |
+
* to a real project instead of the profile directory (the host's cwd).
|
| 27 |
+
*/
|
| 28 |
+
function recentWorkspaces() {
|
| 29 |
+
const paths = [];
|
| 30 |
+
try {
|
| 31 |
+
const raw = readFileSync(joinHome(".dsh", "storages", "workspace.json"), "utf8");
|
| 32 |
+
const data = JSON.parse(raw);
|
| 33 |
+
const workspaces = data?.tables?.workspaces ?? {};
|
| 34 |
+
for (const entry of Object.values(workspaces)) {
|
| 35 |
+
const p = entry?.path;
|
| 36 |
+
if (typeof p === "string" && p && !paths.includes(p)) paths.push(p);
|
| 37 |
+
}
|
| 38 |
+
} catch {
|
| 39 |
+
// storage missing or not JSON — fall through
|
| 40 |
+
}
|
| 41 |
+
return paths;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
function joinHome(...parts) {
|
| 45 |
+
return [HOME, ...parts].join("/");
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
/** Pick a sensible default project root: first DSH workspace, else home. */
|
| 49 |
+
function defaultProjectPath() {
|
| 50 |
+
const workspaces = recentWorkspaces();
|
| 51 |
+
return workspaces[0] || HOME;
|
| 52 |
+
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Resolve a real Node.js binary. Inside the Electron host `process.execPath`
|
|
|
|
| 149 |
},
|
| 150 |
};
|
| 151 |
|
| 152 |
+
const workspacesRoute = {
|
| 153 |
+
kind: "exact",
|
| 154 |
+
path: "/agentlens/workspaces",
|
| 155 |
+
async handler(req, res) {
|
| 156 |
+
try {
|
| 157 |
+
json(res, 200, { ok: true, workspaces: recentWorkspaces(), fallback: fallbackCwd });
|
| 158 |
+
} catch (error) {
|
| 159 |
+
json(res, 500, { ok: false, error: String((error && error.message) || error) });
|
| 160 |
+
}
|
| 161 |
+
},
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
const installRoute = {
|
| 165 |
kind: "exact",
|
| 166 |
path: "/agentlens/install",
|
|
|
|
| 175 |
};
|
| 176 |
|
| 177 |
const disposers = [];
|
| 178 |
+
for (const route of [scanRoute, migrateRoute, installRoute, workspacesRoute]) {
|
| 179 |
try {
|
| 180 |
disposers.push(server.register(route));
|
| 181 |
} catch (error) {
|
|
|
|
| 191 |
*/
|
| 192 |
export function apply(ctx, config = {}) {
|
| 193 |
const entry = resolveEntry(config);
|
| 194 |
+
const fallbackCwd = defaultProjectPath();
|
| 195 |
|
| 196 |
// --- Local HTTP routes for the client UI ---
|
| 197 |
ctx.effect(() => {
|
src/cli/commands/drift.ts
CHANGED
|
@@ -3,30 +3,8 @@ import { resolve, join } from "node:path";
|
|
| 3 |
import type { Rule } from "../../core/types.js";
|
| 4 |
import { parsers } from "../../parsers/index.js";
|
| 5 |
import { detectDriftEntries, findMissingRules } from "../../drift/detect.js";
|
|
|
|
| 6 |
|
| 7 |
-
function findAgentFiles(root: string, agent: string): string[] {
|
| 8 |
-
const parser = parsers[agent];
|
| 9 |
-
if (!parser) return [];
|
| 10 |
-
const files: string[] = [];
|
| 11 |
-
for (const pattern of parser.filePatterns) {
|
| 12 |
-
if (pattern.includes("*")) {
|
| 13 |
-
const parts = pattern.split("/");
|
| 14 |
-
const dirParts = parts.slice(0, -1);
|
| 15 |
-
const filePattern = parts[parts.length - 1];
|
| 16 |
-
const dir = resolve(root, ...dirParts);
|
| 17 |
-
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
| 18 |
-
const regex = new RegExp("^" + filePattern.replace(/\*/g, ".*") + "$");
|
| 19 |
-
for (const f of readdirSync(dir)) {
|
| 20 |
-
if (regex.test(f)) files.push(join(dir, f));
|
| 21 |
-
}
|
| 22 |
-
}
|
| 23 |
-
} else {
|
| 24 |
-
const filePath = resolve(root, pattern);
|
| 25 |
-
if (existsSync(filePath)) files.push(filePath);
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
-
return files;
|
| 29 |
-
}
|
| 30 |
|
| 31 |
export async function detectDrift(root: string): Promise<void> {
|
| 32 |
const absRoot = resolve(root);
|
|
|
|
| 3 |
import type { Rule } from "../../core/types.js";
|
| 4 |
import { parsers } from "../../parsers/index.js";
|
| 5 |
import { detectDriftEntries, findMissingRules } from "../../drift/detect.js";
|
| 6 |
+
import { findAgentFiles } from "../../core/files.js";
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
export async function detectDrift(root: string): Promise<void> {
|
| 10 |
const absRoot = resolve(root);
|
src/cli/commands/move.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { dirname, join, resolve } from "node:path";
|
|
| 3 |
import type { Rule, AgentKind } from "../../core/types.js";
|
| 4 |
import { parsers } from "../../parsers/index.js";
|
| 5 |
import { generateConfig, outputFileFor } from "../../migrator/generate.js";
|
|
|
|
|
|
|
| 6 |
|
| 7 |
interface MoveOpts {
|
| 8 |
from: string;
|
|
@@ -12,31 +14,6 @@ interface MoveOpts {
|
|
| 12 |
}
|
| 13 |
|
| 14 |
/** Find all config files for a given agent in the project. */
|
| 15 |
-
function findAgentFiles(root: string, agent: string): string[] {
|
| 16 |
-
const parser = parsers[agent];
|
| 17 |
-
if (!parser) return [];
|
| 18 |
-
const files: string[] = [];
|
| 19 |
-
|
| 20 |
-
for (const pattern of parser.filePatterns) {
|
| 21 |
-
if (pattern.includes("*")) {
|
| 22 |
-
// Glob like ".cursor/rules/*.mdc"
|
| 23 |
-
const parts = pattern.split("/");
|
| 24 |
-
const dirParts = parts.slice(0, -1);
|
| 25 |
-
const filePattern = parts[parts.length - 1];
|
| 26 |
-
const dir = resolve(root, ...dirParts);
|
| 27 |
-
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
| 28 |
-
const regex = new RegExp("^" + filePattern.replace(/\*/g, ".*") + "$");
|
| 29 |
-
for (const f of readdirSync(dir)) {
|
| 30 |
-
if (regex.test(f)) files.push(join(dir, f));
|
| 31 |
-
}
|
| 32 |
-
}
|
| 33 |
-
} else {
|
| 34 |
-
const filePath = resolve(root, pattern);
|
| 35 |
-
if (existsSync(filePath)) files.push(filePath);
|
| 36 |
-
}
|
| 37 |
-
}
|
| 38 |
-
return files;
|
| 39 |
-
}
|
| 40 |
|
| 41 |
/** Parse all rules from source agent files. */
|
| 42 |
function parseRules(root: string, agent: string): Rule[] {
|
|
@@ -59,8 +36,8 @@ export async function moveRules(opts: MoveOpts): Promise<void> {
|
|
| 59 |
|
| 60 |
console.log(`Migrating rules: ${opts.from} → ${opts.to}\n`);
|
| 61 |
|
| 62 |
-
// Parse source rules
|
| 63 |
-
const rules = parseRules(root, opts.from);
|
| 64 |
if (rules.length === 0) {
|
| 65 |
console.log(`⚠ No rules found for agent "${opts.from}" in ${root}`);
|
| 66 |
const files = findAgentFiles(root, opts.from);
|
|
@@ -83,7 +60,14 @@ export async function moveRules(opts: MoveOpts): Promise<void> {
|
|
| 83 |
writeFileSync(outputPath, output, "utf-8");
|
| 84 |
|
| 85 |
console.log(` ✅ Generated ${outputPath}`);
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
// Show preview
|
| 89 |
const preview = output.split("\n").slice(0, 15).join("\n");
|
|
|
|
| 3 |
import type { Rule, AgentKind } from "../../core/types.js";
|
| 4 |
import { parsers } from "../../parsers/index.js";
|
| 5 |
import { generateConfig, outputFileFor } from "../../migrator/generate.js";
|
| 6 |
+
import { dedupeRules } from "../../core/dedupe.js";
|
| 7 |
+
import { findAgentFiles } from "../../core/files.js";
|
| 8 |
|
| 9 |
interface MoveOpts {
|
| 10 |
from: string;
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
/** Find all config files for a given agent in the project. */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
/** Parse all rules from source agent files. */
|
| 19 |
function parseRules(root: string, agent: string): Rule[] {
|
|
|
|
| 36 |
|
| 37 |
console.log(`Migrating rules: ${opts.from} → ${opts.to}\n`);
|
| 38 |
|
| 39 |
+
// Parse source rules (dedupe: an agent may expose several rule sources)
|
| 40 |
+
const rules = dedupeRules(parseRules(root, opts.from));
|
| 41 |
if (rules.length === 0) {
|
| 42 |
console.log(`⚠ No rules found for agent "${opts.from}" in ${root}`);
|
| 43 |
const files = findAgentFiles(root, opts.from);
|
|
|
|
| 60 |
writeFileSync(outputPath, output, "utf-8");
|
| 61 |
|
| 62 |
console.log(` ✅ Generated ${outputPath}`);
|
| 63 |
+
// True same-file case: the rules were read from the very file we just wrote.
|
| 64 |
+
const sourceFile = rules[0]?.source.file;
|
| 65 |
+
const sameFile = sourceFile !== undefined && resolve(sourceFile) === outputPath;
|
| 66 |
+
if (sameFile) {
|
| 67 |
+
console.log(` ${opts.from} 和 ${opts.to} 使用同一个规则文件 ${outputFileFor(toAgent)}——迁移会原地整理(幂等,不重复添加规则)\n`);
|
| 68 |
+
} else {
|
| 69 |
+
console.log(` ${rules.length} rules converted from ${opts.from} format to ${opts.to} format\n`);
|
| 70 |
+
}
|
| 71 |
|
| 72 |
// Show preview
|
| 73 |
const preview = output.split("\n").slice(0, 15).join("\n");
|
src/cli/commands/scan.ts
CHANGED
|
@@ -2,31 +2,8 @@ import { readFileSync, existsSync, readdirSync, statSync } from "node:fs";
|
|
| 2 |
import { resolve, join } from "node:path";
|
| 3 |
import type { Rule } from "../../core/types.js";
|
| 4 |
import { parsers } from "../../parsers/index.js";
|
|
|
|
| 5 |
|
| 6 |
-
function findAgentFiles(root: string, agent: string): string[] {
|
| 7 |
-
const parser = parsers[agent];
|
| 8 |
-
if (!parser) return [];
|
| 9 |
-
const files: string[] = [];
|
| 10 |
-
|
| 11 |
-
for (const pattern of parser.filePatterns) {
|
| 12 |
-
if (pattern.includes("*")) {
|
| 13 |
-
const parts = pattern.split("/");
|
| 14 |
-
const dirParts = parts.slice(0, -1);
|
| 15 |
-
const filePattern = parts[parts.length - 1];
|
| 16 |
-
const dir = resolve(root, ...dirParts);
|
| 17 |
-
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
| 18 |
-
const regex = new RegExp("^" + filePattern.replace(/\*/g, ".*") + "$");
|
| 19 |
-
for (const f of readdirSync(dir)) {
|
| 20 |
-
if (regex.test(f)) files.push(join(dir, f));
|
| 21 |
-
}
|
| 22 |
-
}
|
| 23 |
-
} else {
|
| 24 |
-
const filePath = resolve(root, pattern);
|
| 25 |
-
if (existsSync(filePath)) files.push(filePath);
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
-
return files;
|
| 29 |
-
}
|
| 30 |
|
| 31 |
export async function scanProject(root: string): Promise<void> {
|
| 32 |
const absRoot = resolve(root);
|
|
@@ -62,7 +39,9 @@ export async function scanProject(root: string): Promise<void> {
|
|
| 62 |
}
|
| 63 |
|
| 64 |
if (!foundAny) {
|
| 65 |
-
console.log("
|
|
|
|
|
|
|
| 66 |
for (const [name, parser] of Object.entries(parsers)) {
|
| 67 |
console.log(` ${name}: ${parser.filePatterns.join(", ")}`);
|
| 68 |
}
|
|
|
|
| 2 |
import { resolve, join } from "node:path";
|
| 3 |
import type { Rule } from "../../core/types.js";
|
| 4 |
import { parsers } from "../../parsers/index.js";
|
| 5 |
+
import { findAgentFiles } from "../../core/files.js";
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
export async function scanProject(root: string): Promise<void> {
|
| 9 |
const absRoot = resolve(root);
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
if (!foundAny) {
|
| 42 |
+
console.log(" 这个目录里没有找到任何 Agent 规则文件。");
|
| 43 |
+
console.log(" 请换一个项目目录(通常包含 CLAUDE.md / AGENTS.md / .cursor/rules)。");
|
| 44 |
+
console.log(" Supported:");
|
| 45 |
for (const [name, parser] of Object.entries(parsers)) {
|
| 46 |
console.log(` ${name}: ${parser.filePatterns.join(", ")}`);
|
| 47 |
}
|
src/core/dedupe.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { Rule } from "./types.js";
|
| 2 |
+
|
| 3 |
+
/**
|
| 4 |
+
* Collapse rules with identical normalized content, keeping the first.
|
| 5 |
+
* Needed because some agents expose more than one rule source (e.g. DSH reads
|
| 6 |
+
* both the project AGENTS.md and the global bot-team state file), and writing
|
| 7 |
+
* every duplicate back would grow the file on each migration.
|
| 8 |
+
*/
|
| 9 |
+
export function dedupeRules(rules: Rule[]): Rule[] {
|
| 10 |
+
const seen = new Set<string>();
|
| 11 |
+
const out: Rule[] = [];
|
| 12 |
+
for (const rule of rules) {
|
| 13 |
+
const key = rule.content.replace(/\s+/g, " ").trim();
|
| 14 |
+
if (key.length === 0 || seen.has(key)) continue;
|
| 15 |
+
seen.add(key);
|
| 16 |
+
out.push(rule);
|
| 17 |
+
}
|
| 18 |
+
return out;
|
| 19 |
+
}
|
src/core/files.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
| 2 |
+
import { dirname, join, resolve } from "node:path";
|
| 3 |
+
import { homedir } from "node:os";
|
| 4 |
+
import { parsers } from "../parsers/index.js";
|
| 5 |
+
|
| 6 |
+
/**
|
| 7 |
+
* Resolve every config file for an agent under a project root.
|
| 8 |
+
*
|
| 9 |
+
* Most agents keep rules in project files (CLAUDE.md, AGENTS.md, …). DSH is
|
| 10 |
+
* special: its bot-team instructions live in the global state file
|
| 11 |
+
* `$HOME/.dsh/bot/state.json`, so that file is always included for `dsh`.
|
| 12 |
+
*/
|
| 13 |
+
export function findAgentFiles(root: string, agent: string): string[] {
|
| 14 |
+
const parser = parsers[agent];
|
| 15 |
+
if (!parser) return [];
|
| 16 |
+
const files: string[] = [];
|
| 17 |
+
|
| 18 |
+
for (const pattern of parser.filePatterns) {
|
| 19 |
+
if (pattern.includes("*")) {
|
| 20 |
+
const parts = pattern.split("/");
|
| 21 |
+
const dirParts = parts.slice(0, -1);
|
| 22 |
+
const filePattern = parts[parts.length - 1];
|
| 23 |
+
const dir = resolve(root, ...dirParts);
|
| 24 |
+
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
| 25 |
+
const regex = new RegExp("^" + filePattern.replace(/\*/g, ".*") + "$");
|
| 26 |
+
for (const f of readdirSync(dir)) {
|
| 27 |
+
if (regex.test(f)) files.push(join(dir, f));
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
} else {
|
| 31 |
+
const filePath = resolve(root, pattern);
|
| 32 |
+
if (existsSync(filePath)) files.push(filePath);
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// DSH global bot-team instructions.
|
| 37 |
+
if (agent === "dsh") {
|
| 38 |
+
const statePath = join(homedir(), ".dsh", "bot", "state.json");
|
| 39 |
+
if (existsSync(statePath)) files.push(statePath);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
return files;
|
| 43 |
+
}
|
src/mcp/server.ts
CHANGED
|
@@ -4,34 +4,13 @@ import { z } from "zod";
|
|
| 4 |
import { readFileSync, existsSync, readdirSync, statSync, mkdirSync, writeFileSync } from "node:fs";
|
| 5 |
import { resolve, join, dirname } from "node:path";
|
| 6 |
import { parsers } from "../parsers/index.js";
|
|
|
|
| 7 |
import { generateConfig, outputFileFor } from "../migrator/generate.js";
|
| 8 |
import { detectDriftEntries } from "../drift/detect.js";
|
| 9 |
import { generateReport, formatReport } from "../checker/engine.js";
|
| 10 |
import type { Rule, AgentKind } from "../core/types.js";
|
|
|
|
| 11 |
|
| 12 |
-
function findAgentFiles(root: string, agent: string): string[] {
|
| 13 |
-
const parser = parsers[agent];
|
| 14 |
-
if (!parser) return [];
|
| 15 |
-
const files: string[] = [];
|
| 16 |
-
for (const pattern of parser.filePatterns) {
|
| 17 |
-
if (pattern.includes("*")) {
|
| 18 |
-
const parts = pattern.split("/");
|
| 19 |
-
const dirParts = parts.slice(0, -1);
|
| 20 |
-
const filePattern = parts[parts.length - 1];
|
| 21 |
-
const dir = resolve(root, ...dirParts);
|
| 22 |
-
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
| 23 |
-
const regex = new RegExp("^" + filePattern.replace(/\*/g, ".*") + "$");
|
| 24 |
-
for (const f of readdirSync(dir)) {
|
| 25 |
-
if (regex.test(f)) files.push(join(dir, f));
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
-
} else {
|
| 29 |
-
const filePath = resolve(root, pattern);
|
| 30 |
-
if (existsSync(filePath)) files.push(filePath);
|
| 31 |
-
}
|
| 32 |
-
}
|
| 33 |
-
return files;
|
| 34 |
-
}
|
| 35 |
|
| 36 |
function collectRules(root: string): Array<{ agent: string; rules: Rule[]; files: string[] }> {
|
| 37 |
const results: Array<{ agent: string; rules: Rule[]; files: string[] }> = [];
|
|
@@ -100,10 +79,11 @@ export function createMcpServer(): McpServer {
|
|
| 100 |
if (!parser) return { content: [{ type: "text", text: `Unknown agent: ${from}` }] };
|
| 101 |
|
| 102 |
const files = findAgentFiles(root, from);
|
| 103 |
-
const
|
| 104 |
for (const file of files) {
|
| 105 |
-
|
| 106 |
}
|
|
|
|
| 107 |
|
| 108 |
if (rules.length === 0) {
|
| 109 |
return { content: [{ type: "text", text: `No ${from} rules found in ${root}` }] };
|
|
|
|
| 4 |
import { readFileSync, existsSync, readdirSync, statSync, mkdirSync, writeFileSync } from "node:fs";
|
| 5 |
import { resolve, join, dirname } from "node:path";
|
| 6 |
import { parsers } from "../parsers/index.js";
|
| 7 |
+
import { dedupeRules } from "../core/dedupe.js";
|
| 8 |
import { generateConfig, outputFileFor } from "../migrator/generate.js";
|
| 9 |
import { detectDriftEntries } from "../drift/detect.js";
|
| 10 |
import { generateReport, formatReport } from "../checker/engine.js";
|
| 11 |
import type { Rule, AgentKind } from "../core/types.js";
|
| 12 |
+
import { findAgentFiles } from "../core/files.js";
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
function collectRules(root: string): Array<{ agent: string; rules: Rule[]; files: string[] }> {
|
| 16 |
const results: Array<{ agent: string; rules: Rule[]; files: string[] }> = [];
|
|
|
|
| 79 |
if (!parser) return { content: [{ type: "text", text: `Unknown agent: ${from}` }] };
|
| 80 |
|
| 81 |
const files = findAgentFiles(root, from);
|
| 82 |
+
const parsed: Rule[] = [];
|
| 83 |
for (const file of files) {
|
| 84 |
+
parsed.push(...parser.parse(readFileSync(file, "utf-8"), file));
|
| 85 |
}
|
| 86 |
+
const rules = dedupeRules(parsed);
|
| 87 |
|
| 88 |
if (rules.length === 0) {
|
| 89 |
return { content: [{ type: "text", text: `No ${from} rules found in ${root}` }] };
|
src/parsers/agents-md.ts
CHANGED
|
@@ -25,7 +25,56 @@ function makeAgentsMdParser(agent: "dsh" | "workbuddy" | "zcode" | "mimo"): Conf
|
|
| 25 |
};
|
| 26 |
}
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
export const workbuddyParser = makeAgentsMdParser("workbuddy");
|
| 30 |
export const zcodeParser = makeAgentsMdParser("zcode");
|
| 31 |
export const mimoParser = makeAgentsMdParser("mimo");
|
|
|
|
| 25 |
};
|
| 26 |
}
|
| 27 |
|
| 28 |
+
/**
|
| 29 |
+
* DSH parser: reads both AGENTS.md (project-level, shared standard) and the
|
| 30 |
+
* global bot-team state file `~/.dsh/bot/state.json`, where each bot's
|
| 31 |
+
* `instructions` field is the user's accumulated DSH rules.
|
| 32 |
+
*/
|
| 33 |
+
export const dshParser: ConfigParser = {
|
| 34 |
+
agent: "dsh",
|
| 35 |
+
filePatterns: ["AGENTS.md"],
|
| 36 |
+
|
| 37 |
+
parse(content: string, filePath: string): Rule[] {
|
| 38 |
+
if (filePath.endsWith("state.json")) {
|
| 39 |
+
return parseBotState(content, filePath);
|
| 40 |
+
}
|
| 41 |
+
const blocks = extractRuleBlocks(content);
|
| 42 |
+
return blocks.map((block) => ({
|
| 43 |
+
id: nextRuleId(),
|
| 44 |
+
category: classifyRule(block),
|
| 45 |
+
content: block,
|
| 46 |
+
scope: "project" as const,
|
| 47 |
+
source: { agent: "dsh" as const, file: filePath },
|
| 48 |
+
status: "active" as const,
|
| 49 |
+
}));
|
| 50 |
+
},
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
/** Extract each bot's instructions from the DSH bot-team state file. */
|
| 54 |
+
function parseBotState(content: string, filePath: string): Rule[] {
|
| 55 |
+
let data: any;
|
| 56 |
+
try {
|
| 57 |
+
data = JSON.parse(content);
|
| 58 |
+
} catch {
|
| 59 |
+
return [];
|
| 60 |
+
}
|
| 61 |
+
const bots = data?.bots ?? {};
|
| 62 |
+
const rules: Rule[] = [];
|
| 63 |
+
for (const bot of Object.values<any>(bots)) {
|
| 64 |
+
const name = typeof bot?.name === "string" ? bot.name : "bot";
|
| 65 |
+
const instructions = typeof bot?.instructions === "string" ? bot.instructions.trim() : "";
|
| 66 |
+
if (instructions.length < 10) continue;
|
| 67 |
+
rules.push({
|
| 68 |
+
id: nextRuleId(),
|
| 69 |
+
category: "context",
|
| 70 |
+
content: `【${name}】${instructions}`,
|
| 71 |
+
scope: "global",
|
| 72 |
+
source: { agent: "dsh", file: filePath },
|
| 73 |
+
status: "active",
|
| 74 |
+
});
|
| 75 |
+
}
|
| 76 |
+
return rules;
|
| 77 |
+
}
|
| 78 |
export const workbuddyParser = makeAgentsMdParser("workbuddy");
|
| 79 |
export const zcodeParser = makeAgentsMdParser("zcode");
|
| 80 |
export const mimoParser = makeAgentsMdParser("mimo");
|
tests/parsers-extra.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import { describe, it, expect } from "vitest";
|
| 2 |
import { windsurfParser } from "../src/parsers/windsurf.js";
|
|
|
|
| 3 |
import { copilotParser } from "../src/parsers/copilot.js";
|
| 4 |
|
| 5 |
describe("windsurfParser", () => {
|
|
@@ -45,3 +46,25 @@ describe("parsers index", () => {
|
|
| 45 |
]);
|
| 46 |
});
|
| 47 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import { describe, it, expect } from "vitest";
|
| 2 |
import { windsurfParser } from "../src/parsers/windsurf.js";
|
| 3 |
+
import { dshParser } from "../src/parsers/agents-md.js";
|
| 4 |
import { copilotParser } from "../src/parsers/copilot.js";
|
| 5 |
|
| 6 |
describe("windsurfParser", () => {
|
|
|
|
| 46 |
]);
|
| 47 |
});
|
| 48 |
});
|
| 49 |
+
|
| 50 |
+
describe("dshParser bot state", () => {
|
| 51 |
+
it("extracts bot instructions from state.json", () => {
|
| 52 |
+
const state = JSON.stringify({
|
| 53 |
+
bots: {
|
| 54 |
+
a: { name: "代码 bot", instructions: "你是团队的代码 Bot,负责写代码和加测试。" },
|
| 55 |
+
b: { name: "计划 bot", instructions: "你是计划 Bot,负责拆解计划。" },
|
| 56 |
+
c: { name: "empty", instructions: "" },
|
| 57 |
+
},
|
| 58 |
+
});
|
| 59 |
+
const rules = dshParser.parse(state, "/Users/x/.dsh/bot/state.json");
|
| 60 |
+
expect(rules.length).toBe(2);
|
| 61 |
+
expect(rules[0].content).toContain("代码 bot");
|
| 62 |
+
expect(rules[0].scope).toBe("global");
|
| 63 |
+
expect(rules[0].source.agent).toBe("dsh");
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
it("falls back to markdown parsing for AGENTS.md", () => {
|
| 67 |
+
const rules = dshParser.parse("- Use TypeScript\n- Run tests first", "AGENTS.md");
|
| 68 |
+
expect(rules.length).toBe(2);
|
| 69 |
+
});
|
| 70 |
+
});
|