22
33namespace Binaryk \LaravelRestify \Exceptions \Solutions ;
44
5+ use Illuminate \Http \Request ;
56use Illuminate \Support \Facades \Cache ;
67use Prism \Prism \Prism ;
78use Spatie \Backtrace \Backtrace ;
89use Spatie \Backtrace \Frame ;
910use Throwable ;
1011
11- class OpenAiSolution
12+ class AiSolution
1213{
1314 protected mixed $ solution ;
1415
1516 public function __construct (protected Throwable $ throwable )
1617 {
1718 $ cacheKey = 'restify-solutions- ' .sha1 (
18- $ this ->throwable ::class.
19- $ this ->throwable ->getMessage ().
20- $ this ->throwable ->getFile ().
21- $ this ->throwable ->getLine ().
22- $ this ->throwable ->getTraceAsString ()
23- );
19+ $ this ->throwable ::class.
20+ $ this ->throwable ->getMessage ().
21+ $ this ->throwable ->getFile ().
22+ $ this ->throwable ->getLine ().
23+ $ this ->throwable ->getTraceAsString ()
24+ );
2425
2526 $ this ->solution = Cache::remember ($ cacheKey ,
2627 now ()->addHour (),
27- fn () => trim (Prism::text ()
28+ fn () => trim (Prism::text ()
2829 ->using (config ('restify.ai_solutions.provider ' ), config ('restify.ai_solutions.model ' ))
2930 ->withSystemPrompt ('You are an expert PHP/Laravel developer. Provide concise, actionable solutions in a single paragraph without line breaks, code blocks, or formatting. Your response should be suitable for JSON API responses. ' )
3031 ->withPrompt ($ this ->generatePrompt ($ this ->throwable ))
@@ -33,6 +34,14 @@ public function __construct(protected Throwable $throwable)
3334 );
3435 }
3536
37+ public static function canUse (Request $ request ): bool
38+ {
39+ return $ request ->expectsJson () &&
40+ config ('restify.ai_solutions ' ) &&
41+ config ('app.debug ' ) &&
42+ config ('prism.providers.openai.api_key ' );
43+ }
44+
3645 public function getSolutionTitle (): string
3746 {
3847 return 'AI Generated Solution ' ;
@@ -63,7 +72,7 @@ protected function generatePrompt(Throwable $throwable): string
6372 $ snippet = $ applicationFrame ->getSnippet (15 );
6473
6574 return (string ) view ('restify::prompts.prompt ' , [
66- 'snippet ' => collect ($ snippet )->map (fn ($ line , $ number ) => $ number .' ' .$ line )->join (PHP_EOL ),
75+ 'snippet ' => collect ($ snippet )->map (fn ($ line , $ number ) => $ number .' ' .$ line )->join (PHP_EOL ),
6776 'file ' => $ applicationFrame ->file ,
6877 'line ' => $ applicationFrame ->lineNumber ,
6978 'exception ' => $ throwable ->getMessage (),
@@ -102,7 +111,8 @@ protected function getMethodsFrom($filePath): string
102111 $ methods = [];
103112
104113 // Extract public method signatures using regex
105- preg_match_all ('/public\s+function\s+(\w+)\s*\([^)]*\)(?:\s*:\s*[^{]+)?\s*{/m ' , $ content , $ matches , PREG_OFFSET_CAPTURE );
114+ preg_match_all ('/public\s+function\s+(\w+)\s*\([^)]*\)(?:\s*:\s*[^{]+)?\s*{/m ' , $ content , $ matches ,
115+ PREG_OFFSET_CAPTURE );
106116
107117 foreach ($ matches [0 ] as $ match ) {
108118 $ methodSignature = trim ($ match [0 ]);
0 commit comments