|
| 1 | +package genkit |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// ansiEscape matches ANSI escape sequences for stripping from PTY output. |
| 9 | +var ansiEscape = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]|\x1b[()][A-Z0-9]|\x1b[^[]`) |
| 10 | + |
| 11 | +// stripANSI removes ANSI escape codes from s. |
| 12 | +func stripANSI(s string) string { |
| 13 | + return ansiEscape.ReplaceAllString(s, "") |
| 14 | +} |
| 15 | + |
| 16 | +// promptRegex matches a line starting with ❯ or > (prompt indicators). |
| 17 | +var promptRegex = regexp.MustCompile(`(?m)^[❯>]\s`) |
| 18 | + |
| 19 | +// detectPromptDefault returns true when a standard prompt character appears. |
| 20 | +func detectPromptDefault(output string) bool { |
| 21 | + clean := stripANSI(output) |
| 22 | + return promptRegex.MatchString(clean) |
| 23 | +} |
| 24 | + |
| 25 | +// detectResponseEndDefault returns true when the prompt reappears after content. |
| 26 | +// We require at least some non-whitespace content before the prompt. |
| 27 | +func detectResponseEndDefault(output string) bool { |
| 28 | + clean := stripANSI(output) |
| 29 | + // Find prompt positions |
| 30 | + locs := promptRegex.FindAllStringIndex(clean, -1) |
| 31 | + if len(locs) < 2 { |
| 32 | + return false |
| 33 | + } |
| 34 | + // Check there's non-whitespace content between first and second prompt. |
| 35 | + between := clean[locs[0][1]:locs[1][0]] |
| 36 | + return strings.TrimSpace(between) != "" |
| 37 | +} |
| 38 | + |
| 39 | +// parseResponseDefault strips ANSI, trims whitespace, and removes spinner lines. |
| 40 | +func parseResponseDefault(raw string) string { |
| 41 | + clean := stripANSI(raw) |
| 42 | + var lines []string |
| 43 | + for _, line := range strings.Split(clean, "\n") { |
| 44 | + trimmed := strings.TrimSpace(line) |
| 45 | + // Skip empty lines, prompt lines, and spinner/status indicators. |
| 46 | + if trimmed == "" || promptRegex.MatchString(line) { |
| 47 | + continue |
| 48 | + } |
| 49 | + lines = append(lines, trimmed) |
| 50 | + } |
| 51 | + return strings.Join(lines, "\n") |
| 52 | +} |
| 53 | + |
| 54 | +// ── Claude Code ────────────────────────────────────────────────────────────── |
| 55 | + |
| 56 | +// ClaudeCodeAdapter drives the `claude` CLI. |
| 57 | +type ClaudeCodeAdapter struct{} |
| 58 | + |
| 59 | +func (ClaudeCodeAdapter) Name() string { return "claude_code" } |
| 60 | +func (ClaudeCodeAdapter) Binary() string { return "claude" } |
| 61 | + |
| 62 | +func (ClaudeCodeAdapter) NonInteractiveArgs(msg string) []string { |
| 63 | + return []string{"-p", msg, "--output-format", "text"} |
| 64 | +} |
| 65 | + |
| 66 | +func (ClaudeCodeAdapter) HealthCheckArgs() []string { |
| 67 | + return []string{"-p", "say ok", "--output-format", "text"} |
| 68 | +} |
| 69 | + |
| 70 | +func (ClaudeCodeAdapter) DetectPrompt(output string) bool { |
| 71 | + return detectPromptDefault(output) |
| 72 | +} |
| 73 | + |
| 74 | +func (ClaudeCodeAdapter) DetectResponseEnd(output string) bool { |
| 75 | + return detectResponseEndDefault(output) |
| 76 | +} |
| 77 | + |
| 78 | +func (ClaudeCodeAdapter) ParseResponse(raw string) string { |
| 79 | + return parseResponseDefault(raw) |
| 80 | +} |
| 81 | + |
| 82 | +// ── Copilot CLI ─────────────────────────────────────────────────────────────── |
| 83 | + |
| 84 | +// CopilotCLIAdapter drives the `copilot` CLI. |
| 85 | +type CopilotCLIAdapter struct{} |
| 86 | + |
| 87 | +func (CopilotCLIAdapter) Name() string { return "copilot_cli" } |
| 88 | +func (CopilotCLIAdapter) Binary() string { return "copilot" } |
| 89 | + |
| 90 | +func (CopilotCLIAdapter) NonInteractiveArgs(msg string) []string { |
| 91 | + return []string{"-p", msg} |
| 92 | +} |
| 93 | + |
| 94 | +func (CopilotCLIAdapter) HealthCheckArgs() []string { |
| 95 | + return []string{"-p", "say ok"} |
| 96 | +} |
| 97 | + |
| 98 | +var copilotPromptRegex = regexp.MustCompile(`(?m)^>\s`) |
| 99 | + |
| 100 | +func (CopilotCLIAdapter) DetectPrompt(output string) bool { |
| 101 | + clean := stripANSI(output) |
| 102 | + return copilotPromptRegex.MatchString(clean) |
| 103 | +} |
| 104 | + |
| 105 | +func (CopilotCLIAdapter) DetectResponseEnd(output string) bool { |
| 106 | + clean := stripANSI(output) |
| 107 | + locs := copilotPromptRegex.FindAllStringIndex(clean, -1) |
| 108 | + if len(locs) < 2 { |
| 109 | + return false |
| 110 | + } |
| 111 | + between := clean[locs[0][1]:locs[1][0]] |
| 112 | + return strings.TrimSpace(between) != "" |
| 113 | +} |
| 114 | + |
| 115 | +func (CopilotCLIAdapter) ParseResponse(raw string) string { |
| 116 | + clean := stripANSI(raw) |
| 117 | + var lines []string |
| 118 | + for _, line := range strings.Split(clean, "\n") { |
| 119 | + trimmed := strings.TrimSpace(line) |
| 120 | + if trimmed == "" || copilotPromptRegex.MatchString(line) { |
| 121 | + continue |
| 122 | + } |
| 123 | + lines = append(lines, trimmed) |
| 124 | + } |
| 125 | + return strings.Join(lines, "\n") |
| 126 | +} |
| 127 | + |
| 128 | +// ── Codex CLI ───────────────────────────────────────────────────────────────── |
| 129 | + |
| 130 | +// CodexCLIAdapter drives the `codex` CLI. |
| 131 | +type CodexCLIAdapter struct{} |
| 132 | + |
| 133 | +func (CodexCLIAdapter) Name() string { return "codex_cli" } |
| 134 | +func (CodexCLIAdapter) Binary() string { return "codex" } |
| 135 | + |
| 136 | +func (CodexCLIAdapter) NonInteractiveArgs(msg string) []string { |
| 137 | + return []string{"exec", msg} |
| 138 | +} |
| 139 | + |
| 140 | +func (CodexCLIAdapter) HealthCheckArgs() []string { |
| 141 | + return []string{"exec", "say ok"} |
| 142 | +} |
| 143 | + |
| 144 | +// codexPromptRegex matches Codex's composer input area indicator. |
| 145 | +var codexPromptRegex = regexp.MustCompile(`(?m)^[>❯]\s|composer|Type your`) |
| 146 | + |
| 147 | +func (CodexCLIAdapter) DetectPrompt(output string) bool { |
| 148 | + clean := stripANSI(output) |
| 149 | + return codexPromptRegex.MatchString(clean) |
| 150 | +} |
| 151 | + |
| 152 | +func (CodexCLIAdapter) DetectResponseEnd(output string) bool { |
| 153 | + return detectResponseEndDefault(output) |
| 154 | +} |
| 155 | + |
| 156 | +func (CodexCLIAdapter) ParseResponse(raw string) string { |
| 157 | + return parseResponseDefault(raw) |
| 158 | +} |
| 159 | + |
| 160 | +// ── Gemini CLI ──────────────────────────────────────────────────────────────── |
| 161 | + |
| 162 | +// GeminiCLIAdapter drives the `gemini` CLI. |
| 163 | +type GeminiCLIAdapter struct{} |
| 164 | + |
| 165 | +func (GeminiCLIAdapter) Name() string { return "gemini_cli" } |
| 166 | +func (GeminiCLIAdapter) Binary() string { return "gemini" } |
| 167 | + |
| 168 | +func (GeminiCLIAdapter) NonInteractiveArgs(msg string) []string { |
| 169 | + return []string{"-p", msg} |
| 170 | +} |
| 171 | + |
| 172 | +func (GeminiCLIAdapter) HealthCheckArgs() []string { |
| 173 | + return []string{"-p", "say ok"} |
| 174 | +} |
| 175 | + |
| 176 | +func (GeminiCLIAdapter) DetectPrompt(output string) bool { |
| 177 | + return detectPromptDefault(output) |
| 178 | +} |
| 179 | + |
| 180 | +func (GeminiCLIAdapter) DetectResponseEnd(output string) bool { |
| 181 | + return detectResponseEndDefault(output) |
| 182 | +} |
| 183 | + |
| 184 | +func (GeminiCLIAdapter) ParseResponse(raw string) string { |
| 185 | + return parseResponseDefault(raw) |
| 186 | +} |
| 187 | + |
| 188 | +// ── Cursor CLI ──────────────────────────────────────────────────────────────── |
| 189 | + |
| 190 | +// CursorCLIAdapter drives the `agent` binary (Cursor's agent CLI). |
| 191 | +type CursorCLIAdapter struct{} |
| 192 | + |
| 193 | +func (CursorCLIAdapter) Name() string { return "cursor_cli" } |
| 194 | +func (CursorCLIAdapter) Binary() string { return "agent" } |
| 195 | + |
| 196 | +func (CursorCLIAdapter) NonInteractiveArgs(msg string) []string { |
| 197 | + return []string{"-p", msg} |
| 198 | +} |
| 199 | + |
| 200 | +func (CursorCLIAdapter) HealthCheckArgs() []string { |
| 201 | + return []string{"-p", "say ok"} |
| 202 | +} |
| 203 | + |
| 204 | +var cursorPromptRegex = regexp.MustCompile(`(?m)^>\s`) |
| 205 | + |
| 206 | +func (CursorCLIAdapter) DetectPrompt(output string) bool { |
| 207 | + clean := stripANSI(output) |
| 208 | + return cursorPromptRegex.MatchString(clean) |
| 209 | +} |
| 210 | + |
| 211 | +func (CursorCLIAdapter) DetectResponseEnd(output string) bool { |
| 212 | + clean := stripANSI(output) |
| 213 | + locs := cursorPromptRegex.FindAllStringIndex(clean, -1) |
| 214 | + if len(locs) < 2 { |
| 215 | + return false |
| 216 | + } |
| 217 | + between := clean[locs[0][1]:locs[1][0]] |
| 218 | + return strings.TrimSpace(between) != "" |
| 219 | +} |
| 220 | + |
| 221 | +func (CursorCLIAdapter) ParseResponse(raw string) string { |
| 222 | + clean := stripANSI(raw) |
| 223 | + var lines []string |
| 224 | + for _, line := range strings.Split(clean, "\n") { |
| 225 | + trimmed := strings.TrimSpace(line) |
| 226 | + if trimmed == "" || cursorPromptRegex.MatchString(line) { |
| 227 | + continue |
| 228 | + } |
| 229 | + lines = append(lines, trimmed) |
| 230 | + } |
| 231 | + return strings.Join(lines, "\n") |
| 232 | +} |
0 commit comments