@aaronshaf/ger 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,373 @@
1
+ import { Context, Data, Effect, Layer } from 'effect'
2
+ import { Console } from 'effect'
3
+ import { exec } from 'node:child_process'
4
+ import { promisify } from 'node:util'
5
+
6
+ const execAsync = promisify(exec)
7
+
8
+ // Dynamic import for Claude SDK
9
+ const getClaudeSDK = () =>
10
+ Effect.tryPromise({
11
+ try: () => import('@anthropic-ai/claude-code'),
12
+ catch: () => null,
13
+ }).pipe(Effect.orElseSucceed(() => null))
14
+
15
+ // Simple strategy focused only on review needs
16
+ export class ReviewStrategyError extends Data.TaggedError('ReviewStrategyError')<{
17
+ message: string
18
+ cause?: unknown
19
+ }> {}
20
+
21
+ // Review strategy interface - focused on specific review patterns
22
+ export interface ReviewStrategy {
23
+ readonly name: string
24
+ readonly isAvailable: () => Effect.Effect<boolean, never>
25
+ readonly executeReview: (
26
+ prompt: string,
27
+ options?: { cwd?: string; systemPrompt?: string },
28
+ ) => Effect.Effect<string, ReviewStrategyError>
29
+ }
30
+
31
+ // Strategy implementations for different AI tools
32
+ export const claudeCliStrategy: ReviewStrategy = {
33
+ name: 'Claude CLI',
34
+ isAvailable: () =>
35
+ Effect.gen(function* () {
36
+ const result = yield* Effect.tryPromise({
37
+ try: () => execAsync('which claude'),
38
+ catch: () => null,
39
+ }).pipe(Effect.orElseSucceed(() => null))
40
+
41
+ return Boolean(result && result.stdout.trim())
42
+ }),
43
+ executeReview: (prompt, options = {}) =>
44
+ Effect.gen(function* () {
45
+ const result = yield* Effect.tryPromise({
46
+ try: async () => {
47
+ const child = require('node:child_process').spawn('claude -p', {
48
+ shell: true,
49
+ stdio: ['pipe', 'pipe', 'pipe'],
50
+ cwd: options.cwd || process.cwd(),
51
+ })
52
+
53
+ child.stdin.write(prompt)
54
+ child.stdin.end()
55
+
56
+ let stdout = ''
57
+ let stderr = ''
58
+
59
+ child.stdout.on('data', (data: Buffer) => {
60
+ stdout += data.toString()
61
+ })
62
+
63
+ child.stderr.on('data', (data: Buffer) => {
64
+ stderr += data.toString()
65
+ })
66
+
67
+ return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
68
+ child.on('close', (code: number) => {
69
+ if (code !== 0) {
70
+ reject(new Error(`Claude CLI exited with code ${code}: ${stderr}`))
71
+ } else {
72
+ resolve({ stdout, stderr })
73
+ }
74
+ })
75
+
76
+ child.on('error', reject)
77
+ })
78
+ },
79
+ catch: (error) =>
80
+ new ReviewStrategyError({
81
+ message: `Claude CLI failed: ${error instanceof Error ? error.message : String(error)}`,
82
+ cause: error,
83
+ }),
84
+ })
85
+
86
+ // Extract response from <response> tags or use full output
87
+ const responseMatch = result.stdout.match(/<response>([\s\S]*?)<\/response>/i)
88
+ return responseMatch ? responseMatch[1].trim() : result.stdout.trim()
89
+ }),
90
+ }
91
+
92
+ export const geminiCliStrategy: ReviewStrategy = {
93
+ name: 'Gemini CLI',
94
+ isAvailable: () =>
95
+ Effect.gen(function* () {
96
+ const result = yield* Effect.tryPromise({
97
+ try: () => execAsync('which gemini'),
98
+ catch: () => null,
99
+ }).pipe(Effect.orElseSucceed(() => null))
100
+
101
+ return Boolean(result && result.stdout.trim())
102
+ }),
103
+ executeReview: (prompt, options = {}) =>
104
+ Effect.gen(function* () {
105
+ const result = yield* Effect.tryPromise({
106
+ try: async () => {
107
+ const child = require('node:child_process').spawn('gemini -p', {
108
+ shell: true,
109
+ stdio: ['pipe', 'pipe', 'pipe'],
110
+ cwd: options.cwd || process.cwd(),
111
+ })
112
+
113
+ child.stdin.write(prompt)
114
+ child.stdin.end()
115
+
116
+ let stdout = ''
117
+ let stderr = ''
118
+
119
+ child.stdout.on('data', (data: Buffer) => {
120
+ stdout += data.toString()
121
+ })
122
+
123
+ child.stderr.on('data', (data: Buffer) => {
124
+ stderr += data.toString()
125
+ })
126
+
127
+ return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
128
+ child.on('close', (code: number) => {
129
+ if (code !== 0) {
130
+ reject(new Error(`Gemini CLI exited with code ${code}: ${stderr}`))
131
+ } else {
132
+ resolve({ stdout, stderr })
133
+ }
134
+ })
135
+
136
+ child.on('error', reject)
137
+ })
138
+ },
139
+ catch: (error) =>
140
+ new ReviewStrategyError({
141
+ message: `Gemini CLI failed: ${error instanceof Error ? error.message : String(error)}`,
142
+ cause: error,
143
+ }),
144
+ })
145
+
146
+ return result.stdout.trim()
147
+ }),
148
+ }
149
+
150
+ export const openCodeCliStrategy: ReviewStrategy = {
151
+ name: 'OpenCode CLI',
152
+ isAvailable: () =>
153
+ Effect.gen(function* () {
154
+ const result = yield* Effect.tryPromise({
155
+ try: () => execAsync('which opencode'),
156
+ catch: () => null,
157
+ }).pipe(Effect.orElseSucceed(() => null))
158
+
159
+ return Boolean(result && result.stdout.trim())
160
+ }),
161
+ executeReview: (prompt, options = {}) =>
162
+ Effect.gen(function* () {
163
+ const result = yield* Effect.tryPromise({
164
+ try: async () => {
165
+ const child = require('node:child_process').spawn('opencode -p', {
166
+ shell: true,
167
+ stdio: ['pipe', 'pipe', 'pipe'],
168
+ cwd: options.cwd || process.cwd(),
169
+ })
170
+
171
+ child.stdin.write(prompt)
172
+ child.stdin.end()
173
+
174
+ let stdout = ''
175
+ let stderr = ''
176
+
177
+ child.stdout.on('data', (data: Buffer) => {
178
+ stdout += data.toString()
179
+ })
180
+
181
+ child.stderr.on('data', (data: Buffer) => {
182
+ stderr += data.toString()
183
+ })
184
+
185
+ return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
186
+ child.on('close', (code: number) => {
187
+ if (code !== 0) {
188
+ reject(new Error(`OpenCode CLI exited with code ${code}: ${stderr}`))
189
+ } else {
190
+ resolve({ stdout, stderr })
191
+ }
192
+ })
193
+
194
+ child.on('error', reject)
195
+ })
196
+ },
197
+ catch: (error) =>
198
+ new ReviewStrategyError({
199
+ message: `OpenCode CLI failed: ${error instanceof Error ? error.message : String(error)}`,
200
+ cause: error,
201
+ }),
202
+ })
203
+
204
+ return result.stdout.trim()
205
+ }),
206
+ }
207
+
208
+ export const codexCliStrategy: ReviewStrategy = {
209
+ name: 'Codex CLI',
210
+ isAvailable: () =>
211
+ Effect.gen(function* () {
212
+ const result = yield* Effect.tryPromise({
213
+ try: () => execAsync('which codex'),
214
+ catch: () => null,
215
+ }).pipe(Effect.orElseSucceed(() => null))
216
+
217
+ return Boolean(result && result.stdout.trim())
218
+ }),
219
+ executeReview: (prompt, options = {}) =>
220
+ Effect.gen(function* () {
221
+ const command = `codex exec "${prompt.replace(/"/g, '\\"')}"`
222
+
223
+ const result = yield* Effect.tryPromise({
224
+ try: () => execAsync(command, { cwd: options.cwd }),
225
+ catch: (error) =>
226
+ new ReviewStrategyError({
227
+ message: `Codex CLI failed: ${error instanceof Error ? error.message : String(error)}`,
228
+ cause: error,
229
+ }),
230
+ })
231
+
232
+ return result.stdout.trim()
233
+ }),
234
+ }
235
+
236
+ export const claudeSDKStrategy: ReviewStrategy = {
237
+ name: 'Claude SDK',
238
+ isAvailable: () =>
239
+ Effect.gen(function* () {
240
+ const sdk = yield* getClaudeSDK()
241
+
242
+ // Check if we have ANTHROPIC_API_KEY
243
+ const hasApiKey = Boolean(process.env.ANTHROPIC_API_KEY)
244
+
245
+ return Boolean(sdk && hasApiKey)
246
+ }),
247
+ executeReview: (prompt, options = {}) =>
248
+ Effect.gen(function* () {
249
+ const sdk = yield* getClaudeSDK()
250
+
251
+ if (!sdk) {
252
+ return yield* Effect.fail(
253
+ new ReviewStrategyError({
254
+ message: 'Claude SDK not available',
255
+ }),
256
+ )
257
+ }
258
+
259
+ const result = yield* Effect.tryPromise({
260
+ try: async () => {
261
+ const messages = []
262
+
263
+ for await (const message of sdk.query({
264
+ prompt,
265
+ options: {
266
+ maxTurns: 3,
267
+ customSystemPrompt:
268
+ options.systemPrompt ||
269
+ 'You are a code review expert. Analyze code changes and provide constructive feedback.',
270
+ allowedTools: ['Read', 'Grep', 'Glob'],
271
+ cwd: options.cwd,
272
+ },
273
+ })) {
274
+ if (message.type === 'result' && message.subtype === 'success') {
275
+ return message.result
276
+ }
277
+ }
278
+
279
+ throw new Error('No result received from Claude SDK')
280
+ },
281
+ catch: (error) =>
282
+ new ReviewStrategyError({
283
+ message: `Claude SDK failed: ${error instanceof Error ? error.message : String(error)}`,
284
+ cause: error,
285
+ }),
286
+ })
287
+
288
+ return result
289
+ }),
290
+ }
291
+
292
+ // Review service using strategy pattern
293
+ export class ReviewStrategyService extends Context.Tag('ReviewStrategyService')<
294
+ ReviewStrategyService,
295
+ {
296
+ readonly getAvailableStrategies: () => Effect.Effect<ReviewStrategy[], never>
297
+ readonly selectStrategy: (
298
+ preferredName?: string,
299
+ ) => Effect.Effect<ReviewStrategy, ReviewStrategyError>
300
+ readonly executeWithStrategy: (
301
+ strategy: ReviewStrategy,
302
+ prompt: string,
303
+ options?: { cwd?: string; systemPrompt?: string },
304
+ ) => Effect.Effect<string, ReviewStrategyError>
305
+ }
306
+ >() {}
307
+
308
+ export const ReviewStrategyServiceLive = Layer.succeed(
309
+ ReviewStrategyService,
310
+ ReviewStrategyService.of({
311
+ getAvailableStrategies: () =>
312
+ Effect.gen(function* () {
313
+ const strategies = [
314
+ claudeSDKStrategy,
315
+ claudeCliStrategy,
316
+ geminiCliStrategy,
317
+ openCodeCliStrategy,
318
+ codexCliStrategy,
319
+ ]
320
+ const available: ReviewStrategy[] = []
321
+
322
+ for (const strategy of strategies) {
323
+ const isAvailable = yield* strategy.isAvailable()
324
+ if (isAvailable) {
325
+ available.push(strategy)
326
+ }
327
+ }
328
+
329
+ return available
330
+ }),
331
+
332
+ selectStrategy: (preferredName?: string) =>
333
+ Effect.gen(function* () {
334
+ const strategies = [
335
+ claudeSDKStrategy,
336
+ claudeCliStrategy,
337
+ geminiCliStrategy,
338
+ openCodeCliStrategy,
339
+ codexCliStrategy,
340
+ ]
341
+ const available: ReviewStrategy[] = []
342
+
343
+ for (const strategy of strategies) {
344
+ const isAvailable = yield* strategy.isAvailable()
345
+ if (isAvailable) {
346
+ available.push(strategy)
347
+ }
348
+ }
349
+
350
+ if (available.length === 0) {
351
+ return yield* Effect.fail(
352
+ new ReviewStrategyError({
353
+ message: 'No AI tools available. Please install claude, gemini, or codex CLI.',
354
+ }),
355
+ )
356
+ }
357
+
358
+ if (preferredName) {
359
+ const preferred = available.find((s: ReviewStrategy) =>
360
+ s.name.toLowerCase().includes(preferredName.toLowerCase()),
361
+ )
362
+ if (preferred) {
363
+ return preferred
364
+ }
365
+ }
366
+
367
+ return available[0] // Return first available
368
+ }),
369
+
370
+ executeWithStrategy: (strategy, prompt, options = {}) =>
371
+ strategy.executeReview(prompt, options),
372
+ }),
373
+ )