File size: 3,808 Bytes
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
031fd6d
 
 
 
 
 
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
031fd6d
 
 
 
 
 
 
 
 
 
 
 
 
 
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
031fd6d
 
 
 
 
7bedc79
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { Rule, AgentKind } from "../core/types.js";

/**
 * Generate agent-specific config file content from unified IR rules.
 */
export function generateConfig(rules: Rule[], target: AgentKind): string {
  switch (target) {
    case "claude":
      return generateClaude(rules);
    case "cursor":
      return generateCursor(rules);
    case "codex":
      return generateCodex(rules);
    case "windsurf":
      return generateWindsurf(rules);
    case "copilot":
      return generateCopilot(rules);
    case "dsh":
    case "workbuddy":
    case "zcode":
    case "mimo":
      // These agents all read the AGENTS.md standard (markdown instructions).
      return generateAgentsMd(rules);
    default:
      return generateGeneric(rules);
  }
}

function groupByCategory(rules: Rule[]): Map<string, Rule[]> {
  const groups = new Map<string, Rule[]>();
  for (const rule of rules) {
    const key = rule.category;
    if (!groups.has(key)) groups.set(key, []);
    groups.get(key)!.push(rule);
  }
  return groups;
}

const CATEGORY_TITLES: Record<string, string> = {
  style: "Code Style",
  workflow: "Workflow",
  constraint: "Constraints",
  tooling: "Tools & Dependencies",
  context: "Project Context",
};

function generateClaude(rules: Rule[]): string {
  const groups = groupByCategory(rules);
  const lines: string[] = ["# Project Rules\n"];
  for (const [cat, items] of groups) {
    lines.push(`## ${CATEGORY_TITLES[cat] ?? cat}\n`);
    for (const r of items) {
      lines.push(`- ${r.content}`);
    }
    lines.push("");
  }
  return lines.join("\n");
}

function generateCursor(rules: Rule[]): string {
  // Output as .mdc format with frontmatter
  const groups = groupByCategory(rules);
  const lines: string[] = [
    "---",
    "description: Migrated rules (generated by agentlens)",
    `globs: ["**/*"]`,
    "---",
    "",
  ];
  for (const [cat, items] of groups) {
    lines.push(`## ${CATEGORY_TITLES[cat] ?? cat}\n`);
    for (const r of items) {
      lines.push(`- ${r.content}`);
    }
    lines.push("");
  }
  return lines.join("\n");
}

/** AGENTS.md standard — shared by Codex / DSH / WorkBuddy / ZCode / MiMo Desktop. */
function generateAgentsMd(rules: Rule[]): string {
  const groups = groupByCategory(rules);
  const lines: string[] = ["# Agent Instructions\n"];
  for (const [cat, items] of groups) {
    lines.push(`## ${CATEGORY_TITLES[cat] ?? cat}\n`);
    for (const r of items) {
      lines.push(`- ${r.content}`);
    }
    lines.push("");
  }
  return lines.join("\n");
}

function generateCodex(rules: Rule[]): string {
  const groups = groupByCategory(rules);
  const lines: string[] = ["# Agent Instructions\n"];
  for (const [cat, items] of groups) {
    lines.push(`## ${CATEGORY_TITLES[cat] ?? cat}\n`);
    for (const r of items) {
      lines.push(`- ${r.content}`);
    }
    lines.push("");
  }
  return lines.join("\n");
}

function generateWindsurf(rules: Rule[]): string {
  // .windsurfrules is plain markdown
  return generateClaude(rules);
}

function generateCopilot(rules: Rule[]): string {
  // .github/copilot-instructions.md is plain markdown
  return generateClaude(rules);
}

function generateGeneric(rules: Rule[]): string {
  return rules.map((r) => `- ${r.content}`).join("\n") + "\n";
}

/**
 * Get the default output file path for a target agent.
 */
export function outputFileFor(target: AgentKind): string {
  switch (target) {
    case "claude": return "CLAUDE.md";
    case "cursor": return ".cursor/rules/agentlens.mdc";
    case "codex": return "AGENTS.md";
    case "windsurf": return ".windsurfrules";
    case "copilot": return ".github/copilot-instructions.md";
    case "dsh":
    case "workbuddy":
    case "zcode":
    case "mimo":
      return "AGENTS.md";
    default: return "agentlens-rules.md";
  }
}