164 lines
4.9 KiB
PHP
Executable File
164 lines
4.9 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\PromptProject;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\PromptProject\StorePromptProjectRequest;
|
|
use App\Http\Requests\PromptProject\UpdatePromptProjectRequest;
|
|
use App\Models\AiProvider;
|
|
use App\Services\PromptProjectService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\View\View;
|
|
|
|
class PromptProjectController extends Controller
|
|
{
|
|
/**
|
|
* Create a new controller instance.
|
|
*/
|
|
public function __construct(
|
|
private readonly PromptProjectService $promptProjectService
|
|
) {}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(): View
|
|
{
|
|
$result = $this->promptProjectService->index();
|
|
$projects = $result['data'] ?? [];
|
|
|
|
return view('prompt-projects.index', compact('projects'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(): View
|
|
{
|
|
$this->authorize('create', \App\Models\PromptProject::class);
|
|
|
|
$providers = AiProvider::where('is_active', true)->get();
|
|
|
|
return view('prompt-projects.create', compact('providers'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StorePromptProjectRequest $request): RedirectResponse
|
|
{
|
|
$this->authorize('create', \App\Models\PromptProject::class);
|
|
|
|
$this->promptProjectService->store($request->validated());
|
|
|
|
return redirect()->route('prompt-projects.index')
|
|
->with('success', 'Project created successfully.');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(\App\Models\PromptProject $promptProject): View
|
|
{
|
|
$this->authorize('view', $promptProject);
|
|
|
|
$providerModels = \App\Models\AiModel::where('provider_id', $promptProject->provider_id)
|
|
->where('is_active', true)
|
|
->get();
|
|
|
|
$models = $providerModels->isNotEmpty()
|
|
? $providerModels
|
|
: \App\Models\AiModel::where('is_active', true)->get();
|
|
|
|
$generations = \App\Models\PromptGeneration::where('project_id', $promptProject->id)
|
|
->latest()
|
|
->take(5)
|
|
->get();
|
|
|
|
return view('prompt-projects.show', compact('promptProject', 'models', 'generations'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(\App\Models\PromptProject $promptProject): View
|
|
{
|
|
$this->authorize('update', $promptProject);
|
|
|
|
$providers = AiProvider::where('is_active', true)->get();
|
|
|
|
return view('prompt-projects.edit', compact('promptProject', 'providers'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(UpdatePromptProjectRequest $request, \App\Models\PromptProject $promptProject): RedirectResponse
|
|
{
|
|
$this->authorize('update', $promptProject);
|
|
|
|
$this->promptProjectService->update($promptProject, $request->validated());
|
|
|
|
return redirect()->route('prompt-projects.show', $promptProject)
|
|
->with('success', 'Project updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(\App\Models\PromptProject $promptProject): RedirectResponse
|
|
{
|
|
$this->authorize('delete', $promptProject);
|
|
|
|
$this->promptProjectService->destroy($promptProject);
|
|
|
|
return redirect()->route('prompt-projects.index')
|
|
->with('success', 'Project deleted successfully.');
|
|
}
|
|
|
|
/**
|
|
* Generate prompt for the specified project.
|
|
*/
|
|
public function generate(\App\Models\PromptProject $promptProject, Request $request): RedirectResponse
|
|
{
|
|
$this->authorize('update', $promptProject);
|
|
|
|
$model = null;
|
|
if ($request->filled('model')) {
|
|
$modelSlug = $request->input('model');
|
|
|
|
if ($modelSlug === 'outros') {
|
|
$customModel = $request->input('custom_model');
|
|
if ($customModel) {
|
|
$model = $customModel;
|
|
}
|
|
} else {
|
|
$model = \App\Models\AiModel::where('slug', $modelSlug)
|
|
->where('is_active', true)
|
|
->first();
|
|
}
|
|
}
|
|
|
|
$this->promptProjectService->generate($promptProject, $model, Auth::id());
|
|
|
|
return redirect()->route('prompt-projects.show', $promptProject)
|
|
->with('success', 'Prompt generated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Get built prompt for the specified project.
|
|
*/
|
|
public function getPrompt(\App\Models\PromptProject $promptProject): \Illuminate\Http\JsonResponse
|
|
{
|
|
$this->authorize('view', $promptProject);
|
|
|
|
$prompt = $this->promptProjectService->buildPrompt($promptProject);
|
|
|
|
return response()->json(['prompt' => $prompt]);
|
|
}
|
|
}
|