Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Http\Controllers;

use App\Http\Requests\Chat\ChatRequest;
use App\Models\Chat;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;
use OpenAI\Laravel\Facades\OpenAI;
use Illuminate\Http\StreamedEvent;
Expand Down Expand Up @@ -42,13 +44,8 @@ public function show(Chat $chat)
]);
}

public function store(Request $request)
public function store(ChatRequest $request)
{
$request->validate([
'title' => 'nullable|string|max:255',
'firstMessage' => 'nullable|string',
]);

$title = $request->title;

// If no title provided, use "Untitled" initially
Expand All @@ -74,14 +71,10 @@ public function store(Request $request)
return redirect()->route('chat.show', $chat);
}

public function update(Request $request, Chat $chat)
public function update(ChatRequest $request, Chat $chat)
{
$this->authorize('update', $chat);

$request->validate([
'title' => 'required|string|max:255',
]);

$chat->update([
'title' => $request->title,
]);
Expand Down Expand Up @@ -184,12 +177,12 @@ public function stream(Request $request, ?Chat $chat = null)
]);

// Generate title if this is a new chat with "Untitled" title
\Log::info('Checking if should generate title', ['chat_title' => $chat->title]);
Log::info('Checking if should generate title', ['chat_title' => $chat->title]);
if ($chat->title === 'Untitled') {
\Log::info('Generating title in background for chat', ['chat_id' => $chat->id]);
Log::info('Generating title in background for chat', ['chat_id' => $chat->id]);
$this->generateTitleInBackground($chat);
} else {
\Log::info('Not generating title', ['current_title' => $chat->title]);
Log::info('Not generating title', ['current_title' => $chat->title]);
}
}
}, 200, [
Expand All @@ -216,7 +209,7 @@ public function titleStream(Chat $chat)
{
$this->authorize('view', $chat);

\Log::info('Title stream requested for chat', ['chat_id' => $chat->id, 'title' => $chat->title]);
Log::info('Title stream requested for chat', ['chat_id' => $chat->id, 'title' => $chat->title]);

return response()->eventStream(function () use ($chat) {
// If title is already set and not "Untitled", send it immediately
Expand Down Expand Up @@ -295,13 +288,13 @@ private function generateTitleInBackground(Chat $chat)
// Update the chat title
$chat->update(['title' => $generatedTitle]);

\Log::info('Generated title for chat', ['chat_id' => $chat->id, 'title' => $generatedTitle]);
Log::info('Generated title for chat', ['chat_id' => $chat->id, 'title' => $generatedTitle]);

} catch (\Exception $e) {
// Fallback title on error
$fallbackTitle = substr($firstMessage->content, 0, 47) . '...';
$chat->update(['title' => $fallbackTitle]);
\Log::error('Error generating title, using fallback', ['error' => $e->getMessage()]);
Log::error('Error generating title, using fallback', ['error' => $e->getMessage()]);
}
}
}
29 changes: 29 additions & 0 deletions app/Http/Requests/Chat/ChatRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests\Chat;

use Illuminate\Foundation\Http\FormRequest;

class ChatRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'title' => 'nullable|string|max:255',
'firstMessage' => 'nullable|string',
];
}
}
Loading