feat(edit): add markdown file support with tailored prompt

- Introduce check for .md extension and use technical writer system prompt.
- Adjust response cleaning: trim for markdown, use CleanCodeResponse for code.
- Remove nolint comment and unnecessary line skipping in removeLastModifiedComments.
This commit is contained in:
Gregory Gauthier 2026-03-30 12:22:50 +01:00
parent 1c790976ab
commit ba8d9b1d7e

View File

@ -35,7 +35,6 @@ var editCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
// nolint:gosec // intentional file read from user input
original, err := os.ReadFile(filePath) original, err := os.ReadFile(filePath)
if err != nil { if err != nil {
logger.Error("failed to read file", "file", filePath, "error", err) logger.Error("failed to read file", "file", filePath, "error", err)
@ -46,14 +45,28 @@ var editCmd = &cobra.Command{
cleanedOriginal := removeLastModifiedComments(string(original)) cleanedOriginal := removeLastModifiedComments(string(original))
client := grok.NewClient() client := grok.NewClient()
messages := []map[string]string{ isMarkdown := filepath.Ext(filePath) == ".md"
{"role": "system", "content": "You are an expert programmer. Remove all unnecessary comments including last modified timestamps and ownership comments. Return only the cleaned code with no explanations, no markdown, no extra text."}, var messages []map[string]string
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(filePath), cleanedOriginal, instruction)}, if isMarkdown {
messages = []map[string]string{
{"role": "system", "content": "You are an expert technical writer. Edit markdown content clearly and concisely. Return only the edited markdown with no explanations, no markdown formatting, no extra text."},
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(filePath), cleanedOriginal, instruction)},
}
} else {
messages = []map[string]string{
{"role": "system", "content": "You are an expert programmer. Remove all unnecessary comments including last modified timestamps and ownership comments. Return only the cleaned code with no explanations, no markdown, no extra text."},
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(filePath), cleanedOriginal, instruction)},
}
} }
color.Yellow("Asking Grok to %s...\n", instruction) color.Yellow("Asking Grok to %s...\n", instruction)
raw := client.StreamSilent(messages, model) raw := client.StreamSilent(messages, model)
newContent := grok.CleanCodeResponse(raw) var newContent string
if isMarkdown {
newContent = strings.TrimSpace(raw)
} else {
newContent = grok.CleanCodeResponse(raw)
}
color.Green("✓ Response received") color.Green("✓ Response received")
color.Cyan("\nProposed changes:") color.Cyan("\nProposed changes:")
@ -92,9 +105,6 @@ func removeLastModifiedComments(content string) string {
cleanedLines := make([]string, 0, len(lines)) cleanedLines := make([]string, 0, len(lines))
for _, line := range lines { for _, line := range lines {
if strings.Contains(line, "Last modified") {
continue
}
cleanedLines = append(cleanedLines, line) cleanedLines = append(cleanedLines, line)
} }