|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + MarkerStart = "<!-- EFCTL_INSTRUCTIONS_START -->" |
| 12 | + MarkerEnd = "<!-- EFCTL_INSTRUCTIONS_END -->" |
| 13 | +) |
| 14 | + |
| 15 | +// SetupAIInstructions configures instructions for various AI agents idempotently. |
| 16 | +func SetupAIInstructions(agentName string, workspace string) error { |
| 17 | + var targetFile string |
| 18 | + var instructions string |
| 19 | + |
| 20 | + agentName = strings.ToLower(agentName) |
| 21 | + |
| 22 | + switch agentName { |
| 23 | + case "copilot", "cursor": |
| 24 | + targetFile = ".cursorrules" |
| 25 | + instructions = getCopilotInstructions() |
| 26 | + case "claude": |
| 27 | + targetFile = ".clauderules" |
| 28 | + instructions = getClaudeInstructions() |
| 29 | + case "gemini": |
| 30 | + targetFile = filepath.Join(".agents", "instructions.md") |
| 31 | + instructions = getGeminiInstructions() |
| 32 | + default: |
| 33 | + return fmt.Errorf("unsupported agent: %s; supported agents are: copilot, claude, gemini", agentName) |
| 34 | + } |
| 35 | + |
| 36 | + absPath := filepath.Join(workspace, targetFile) |
| 37 | + targetDir := filepath.Dir(absPath) |
| 38 | + if err := os.MkdirAll(targetDir, 0750); err != nil { |
| 39 | + return fmt.Errorf("failed to create directory for %s: %w", targetFile, err) |
| 40 | + } |
| 41 | + |
| 42 | + content := fmt.Sprintf("\n%s\n%s\n%s\n", MarkerStart, strings.TrimSpace(instructions), MarkerEnd) |
| 43 | + |
| 44 | + existing, err := os.ReadFile(absPath) // #nosec G304 |
| 45 | + if err != nil { |
| 46 | + if os.IsNotExist(err) { |
| 47 | + return os.WriteFile(absPath, []byte(strings.TrimSpace(content)+"\n"), 0600) // #nosec G306 G703 |
| 48 | + } |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + existingStr := string(existing) |
| 53 | + startIdx := strings.Index(existingStr, MarkerStart) |
| 54 | + endIdx := strings.Index(existingStr, MarkerEnd) |
| 55 | + |
| 56 | + if startIdx != -1 && endIdx != -1 && endIdx > startIdx { |
| 57 | + // Replace existing block |
| 58 | + newContent := existingStr[:startIdx] + strings.TrimSpace(content) + existingStr[endIdx+len(MarkerEnd):] |
| 59 | + return os.WriteFile(absPath, []byte(newContent), 0600) // #nosec G306 G703 |
| 60 | + } |
| 61 | + |
| 62 | + // Append to file |
| 63 | + newContent := strings.TrimRight(existingStr, "\n") + "\n" + strings.TrimSpace(content) + "\n" |
| 64 | + return os.WriteFile(absPath, []byte(newContent), 0600) // #nosec G306 G703 |
| 65 | +} |
| 66 | + |
| 67 | +func getCopilotInstructions() string { |
| 68 | + return ` |
| 69 | +# efctl Context |
| 70 | +You are working on a project using 'efctl', the EVE Frontier CLI. |
| 71 | +Reference AGENTS.md for core principles. |
| 72 | +
|
| 73 | +## efctl Commands |
| 74 | +- Init: efctl init |
| 75 | +- Up: efctl env up |
| 76 | +- Down: efctl env down |
| 77 | +- Status: efctl env status |
| 78 | +- Publish Extension: efctl env extension publish [contract-path] |
| 79 | +- Query World: efctl world query [object_id] |
| 80 | +` |
| 81 | +} |
| 82 | + |
| 83 | +func getClaudeInstructions() string { |
| 84 | + return getCopilotInstructions() |
| 85 | +} |
| 86 | + |
| 87 | +func getGeminiInstructions() string { |
| 88 | + return getCopilotInstructions() |
| 89 | +} |
0 commit comments