-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebuddy.ts
More file actions
184 lines (160 loc) · 5.39 KB
/
codebuddy.ts
File metadata and controls
184 lines (160 loc) · 5.39 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { join } from 'node:path';
import {
BaseRuleFormatter,
RuleFormatSpec,
RuleFormatCategory,
ParsedRule,
RuleGenerationContext,
RuleGenerationResult
} from '../core/interfaces';
/**
* Tencent Cloud CodeBuddy rule formatter
* Generates .codebuddy/rules/{name}.md files for CodeBuddy AI assistant
* CodeBuddy (腾讯云代码助手) is an AI-powered coding assistant that supports VS Code and JetBrains IDEs
*/
export class CodeBuddyFormatter extends BaseRuleFormatter {
readonly spec: RuleFormatSpec = {
id: 'codebuddy',
name: 'Tencent Cloud CodeBuddy',
category: RuleFormatCategory.DIRECTORY_BASED,
extension: '.md',
supportsMultipleRules: true,
requiresMetadata: true,
defaultPath: '.codebuddy/rules'
};
/**
* Generate rule file for CodeBuddy
*/
async generateRule(
rule: ParsedRule,
context: RuleGenerationContext
): Promise<RuleGenerationResult> {
try {
const filePath = this.getOutputPath(rule, context);
// Check if file exists
await this.checkFileExists(filePath, context.force);
// Ensure directory exists
await this.ensureDirectory(filePath);
// Transform content
const content = this.transformContent(rule);
// Write file
await this.writeFile(filePath, content);
return {
format: this.spec.id,
success: true,
filePath,
ruleName: rule.name
};
} catch (error) {
return {
format: this.spec.id,
success: false,
error: (error as Error).message,
ruleName: rule.name
};
}
}
/**
* Check if rule is compatible with CodeBuddy format
*/
isRuleCompatible(rule: ParsedRule): boolean {
// CodeBuddy supports all rules
return true;
}
/**
* Get output file path for the rule
*/
getOutputPath(rule: ParsedRule, context: RuleGenerationContext): string {
const filename = `${rule.name || 'default'}${this.spec.extension}`;
return join(context.outputDir, this.spec.defaultPath, filename);
}
/**
* Transform rule content for CodeBuddy format
*/
protected transformContent(rule: ParsedRule): string {
let content = rule.content;
// Add header with metadata for CodeBuddy
const header = this.createHeader(rule);
// Format content with proper markdown structure
const formattedContent = this.formatContent(content, rule);
return `${header}\n\n${formattedContent}`;
}
/**
* Create header for CodeBuddy rule file
*/
private createHeader(rule: ParsedRule): string {
const lines: string[] = [];
// Add title
lines.push(`# ${rule.name || 'CodeBuddy Development Rule'}`);
lines.push('');
// Add metadata section if metadata exists
if (rule.metadata && Object.keys(rule.metadata).length > 0) {
lines.push('## Metadata');
lines.push('');
lines.push('```yaml');
Object.entries(rule.metadata).forEach(([key, value]) => {
const stringValue = typeof value === 'string' ? value : JSON.stringify(value);
lines.push(`${key}: ${stringValue}`);
});
lines.push('```');
lines.push('');
}
// Add rule type indicator
lines.push('## Rule Type');
lines.push('');
lines.push(`- **Type**: ${rule.isRoot ? 'Global Rule (Always Active)' : 'Project-Specific Rule'}`);
lines.push(`- **AI Assistant**: Tencent Cloud CodeBuddy`);
lines.push(`- **Supported IDEs**: VS Code, JetBrains IDEs`);
lines.push('');
// Add usage instructions
lines.push('## Usage');
lines.push('');
lines.push('This rule will be automatically loaded by CodeBuddy when:');
if (rule.isRoot) {
lines.push('- CodeBuddy is active in your IDE (always applied)');
} else {
lines.push('- You are working in this project directory');
lines.push('- CodeBuddy detects the `.codebuddy` configuration');
}
lines.push('');
return lines.join('\n');
}
/**
* Format the rule content with proper sections
*/
private formatContent(content: string, rule: ParsedRule): string {
const lines: string[] = [];
// Add main rule section
lines.push('## Development Guidelines');
lines.push('');
// Process the content
const contentLines = content.split('\n');
contentLines.forEach(line => {
// Ensure proper markdown formatting
if (line.trim().startsWith('-') || line.trim().startsWith('*')) {
// It's already a list item
lines.push(line);
} else if (line.trim() === '') {
// Empty line
lines.push('');
} else if (line.match(/^#+\s/)) {
// It's a heading, bump it down one level to maintain hierarchy
lines.push('#' + line);
} else {
// Regular text
lines.push(line);
}
});
// Add footer with additional CodeBuddy-specific instructions
lines.push('');
lines.push('---');
lines.push('');
lines.push('### CodeBuddy Integration Notes');
lines.push('');
lines.push('- CodeBuddy will use these guidelines to provide context-aware code suggestions');
lines.push('- The AI assistant will follow these rules when generating code completions');
lines.push('- Use CodeBuddy\'s chat feature to ask questions about these guidelines');
lines.push('- These rules work with CodeBuddy\'s MCP (Model Context Protocol) integration');
return lines.join('\n');
}
}