2026-05-03 12:36:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\App\Ai\GeneratePostContentRequest;
|
|
|
|
|
use App\Jobs\Ai\StreamPostContent;
|
|
|
|
|
use App\Models\Post;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2026-05-03 22:36:26 +00:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2026-05-03 12:36:50 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
class PostAiGenerateController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function generate(GeneratePostContentRequest $request, Post $post): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
2026-05-21 22:27:19 +00:00
|
|
|
$this->authorize('update', $post);
|
2026-05-03 12:36:50 +00:00
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
$gate = Gate::inspect('useAi', $workspace->account);
|
|
|
|
|
if ($gate->denied()) {
|
|
|
|
|
return response()->json(['message' => $gate->message()], Response::HTTP_PAYMENT_REQUIRED);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 12:36:50 +00:00
|
|
|
$generationId = (string) Str::uuid();
|
|
|
|
|
|
|
|
|
|
StreamPostContent::dispatch(
|
|
|
|
|
workspaceId: $workspace->id,
|
|
|
|
|
userId: $request->user()->id,
|
|
|
|
|
generationId: $generationId,
|
|
|
|
|
prompt: $request->string('prompt')->toString(),
|
|
|
|
|
currentContent: $request->input('current_content'),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'generation_id' => $generationId,
|
2026-05-08 21:30:11 +00:00
|
|
|
'channel' => "user.{$request->user()->id}.ai-gen.{$generationId}",
|
2026-05-03 12:36:50 +00:00
|
|
|
], Response::HTTP_ACCEPTED);
|
|
|
|
|
}
|
|
|
|
|
}
|