agentlens / src /parsers /agents-md.ts
hongcoding's picture
feat: DSH bot-instruction parser + dedupe + DSH panel project picker
0fc6e34 verified
Raw History Blame Contribute Delete
2.53 kB
import type { Rule } from "../core/types.js";
import type { ConfigParser } from "./base.js";
import { nextRuleId, classifyRule, extractRuleBlocks } from "./base.js";
/**
* AGENTS.md parser — the shared instruction standard read by Codex, DSH,
* WorkBuddy, ZCode and MiMo Desktop.
*/
function makeAgentsMdParser(agent: "dsh" | "workbuddy" | "zcode" | "mimo"): ConfigParser {
return {
agent,
filePatterns: ["AGENTS.md"],
parse(content: string, filePath: string): Rule[] {
const blocks = extractRuleBlocks(content);
return blocks.map((block) => ({
id: nextRuleId(),
category: classifyRule(block),
content: block,
scope: "project" as const,
source: { agent, file: filePath },
status: "active" as const,
}));
},
};
}
/**
* DSH parser: reads both AGENTS.md (project-level, shared standard) and the
* global bot-team state file `~/.dsh/bot/state.json`, where each bot's
* `instructions` field is the user's accumulated DSH rules.
*/
export const dshParser: ConfigParser = {
agent: "dsh",
filePatterns: ["AGENTS.md"],
parse(content: string, filePath: string): Rule[] {
if (filePath.endsWith("state.json")) {
return parseBotState(content, filePath);
}
const blocks = extractRuleBlocks(content);
return blocks.map((block) => ({
id: nextRuleId(),
category: classifyRule(block),
content: block,
scope: "project" as const,
source: { agent: "dsh" as const, file: filePath },
status: "active" as const,
}));
},
};
/** Extract each bot's instructions from the DSH bot-team state file. */
function parseBotState(content: string, filePath: string): Rule[] {
let data: any;
try {
data = JSON.parse(content);
} catch {
return [];
}
const bots = data?.bots ?? {};
const rules: Rule[] = [];
for (const bot of Object.values<any>(bots)) {
const name = typeof bot?.name === "string" ? bot.name : "bot";
const instructions = typeof bot?.instructions === "string" ? bot.instructions.trim() : "";
if (instructions.length < 10) continue;
rules.push({
id: nextRuleId(),
category: "context",
content: `【${name}】${instructions}`,
scope: "global",
source: { agent: "dsh", file: filePath },
status: "active",
});
}
return rules;
}
export const workbuddyParser = makeAgentsMdParser("workbuddy");
export const zcodeParser = makeAgentsMdParser("zcode");
export const mimoParser = makeAgentsMdParser("mimo");