diff --git a/.agents/skills/developing-with-ai-sdk/SKILL.md b/.agents/skills/developing-with-ai-sdk/SKILL.md new file mode 100644 index 000000000..3e6c70300 --- /dev/null +++ b/.agents/skills/developing-with-ai-sdk/SKILL.md @@ -0,0 +1,413 @@ +--- +name: developing-with-ai-sdk +description: Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter). +--- + +# Developing with the Laravel AI SDK + +The Laravel AI SDK (`laravel/ai`) is the official AI package for Laravel, providing a unified API for agents, images, audio, transcription, embeddings, reranking, vector stores, and file management across multiple AI providers. + +## Searching the Documentation + +This package is new. Always search the documentation before implementing any feature. Never guess at APIs — the documentation is the single source of truth. + +- Use broad, simple queries that match the documentation section headings below. +- Do not add package names to queries — package information is shared automatically. Use `test agent fake`, not `laravel ai test agent fake`. +- Run multiple queries at once — the most relevant results are returned first. + +### Documentation Sections + +Use these section headings as query terms for accurate results: + +- Introduction, Installation, Configuration, Provider Support +- Agents: Prompting, Conversation Context, Structured Output, Attachments, Streaming, Broadcasting, Queueing, Tools, Provider Tools, Middleware, Anonymous Agents, Agent Configuration +- Images +- Audio (TTS) +- Transcription (STT) +- Embeddings: Querying Embeddings, Caching Embeddings +- Reranking +- Files +- Vector Stores: Adding Files to Stores +- Failover +- Testing: Agents, Images, Audio, Transcriptions, Embeddings, Reranking, Files, Vector Stores +- Events + +## Decision Workflow + +Determine the right entry point before writing code: + +Text generation or chat? → Agent class with `Promptable` trait +Chat with conversation history? → Agent + `Conversational` interface (manual) or `RemembersConversations` trait (automatic) +Structured JSON output? → Agent + `HasStructuredOutput` interface +Image generation? → `Image::of()->generate()` +Audio synthesis? → `Audio::of()->generate()` +Transcription? → `Transcription::fromPath()->generate()` +Embeddings? → `Embeddings::for()->generate()` +Reranking? → `Reranking::of()->rerank()` +File storage? → `Document::fromPath()->put()` +Vector stores? → `Stores::create()` + +## Basic Usage Examples + +### Agents + +```php +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Promptable; + +class SalesCoach implements Agent +{ + use Promptable; + + public function instructions(): string + { + return 'You are a sales coach.'; + } +} + +// Prompting +$response = (new SalesCoach)->prompt('Analyze this transcript...'); +echo $response->text; + +// Streaming (returns SSE response from a route) +return (new SalesCoach)->stream('Analyze this transcript...'); + +// Queueing +(new SalesCoach)->queue('Analyze this transcript...') + ->then(fn ($response) => /* ... */); + +// Anonymous agents +use function Laravel\Ai\{agent}; + +$response = agent(instructions: 'You are a helpful assistant.')->prompt('Hello'); +``` + +### Conversation Context + +Manual conversation history via the `Conversational` interface: + +```php +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Contracts\Conversational; +use Laravel\Ai\Messages\Message; +use Laravel\Ai\Promptable; + +class SalesCoach implements Agent, Conversational +{ + use Promptable; + + public function __construct(public User $user) {} + + public function instructions(): string { return 'You are a sales coach.'; } + + public function messages(): iterable + { + return History::where('user_id', $this->user->id) + ->latest()->limit(50)->get()->reverse() + ->map(fn ($m) => new Message($m->role, $m->content)) + ->all(); + } +} +``` + +Automatic conversation persistence via the `RemembersConversations` trait: + +```php +use Laravel\Ai\Concerns\RemembersConversations; +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Contracts\Conversational; +use Laravel\Ai\Promptable; + +class SalesCoach implements Agent, Conversational +{ + use Promptable, RemembersConversations; + + public function instructions(): string { return 'You are a sales coach.'; } +} + +// Start a new conversation +$response = (new SalesCoach)->forUser($user)->prompt('Hello!'); +$conversationId = $response->conversationId; + +// Continue an existing conversation +$response = (new SalesCoach)->continue($conversationId, as: $user)->prompt('Tell me more.'); +``` + +### Structured Output + +```php +use Illuminate\Contracts\JsonSchema\JsonSchema; +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Contracts\HasStructuredOutput; +use Laravel\Ai\Promptable; + +class Reviewer implements Agent, HasStructuredOutput +{ + use Promptable; + + public function instructions(): string { return 'Review and score content.'; } + + public function schema(JsonSchema $schema): array + { + return [ + 'feedback' => $schema->string()->required(), + 'score' => $schema->integer()->min(1)->max(10)->required(), + ]; + } +} + +$response = (new Reviewer)->prompt('Review this...'); +echo $response['score']; // Access like an array +``` + +### Images + +```php +use Laravel\Ai\Image; + +$image = Image::of('A sunset over mountains') + ->landscape() + ->quality('high') + ->generate(); + +$path = $image->store(); // Store to default disk +``` + +### Audio + +```php +use Laravel\Ai\Audio; + +$audio = Audio::of('Hello from Laravel.') + ->female() + ->instructions('Speak warmly') + ->generate(); + +$path = $audio->store(); +``` + +### Transcription + +```php +use Laravel\Ai\Transcription; + +$transcript = Transcription::fromStorage('audio.mp3') + ->diarize() + ->generate(); + +echo (string) $transcript; +``` + +### Embeddings + +```php +use Laravel\Ai\Embeddings; +use Illuminate\Support\Str; + +$response = Embeddings::for(['Text one', 'Text two']) + ->dimensions(1536) + ->cache() + ->generate(); + +// Single string via Stringable +$embedding = Str::of('Napa Valley has great wine.')->toEmbeddings(); +``` + +### Reranking + +```php +use Laravel\Ai\Reranking; + +$response = Reranking::of(['Django is Python.', 'Laravel is PHP.', 'React is JS.']) + ->limit(5) + ->rerank('PHP frameworks'); + +$response->first()->document; // "Laravel is PHP." +``` + +### Files and Vector Stores + +```php +use Laravel\Ai\Files\Document; +use Laravel\Ai\Stores; + +// Store a file with the provider +$file = Document::fromPath('/path/to/doc.pdf')->put(); + +// Create a vector store and add files +$store = Stores::create('Knowledge Base'); +$store->add($file->id); +$store->add(Document::fromStorage('manual.pdf')); // Store + add in one step +``` + +## Agent Configuration + +### PHP Attributes + +```php +use Laravel\Ai\Attributes\{Provider, MaxSteps, MaxTokens, Temperature, Timeout}; + +#[Provider('anthropic')] +#[MaxSteps(10)] +#[MaxTokens(4096)] +#[Temperature(0.7)] +#[Timeout(120)] +class MyAgent implements Agent +{ + use Promptable; + // ... +} +``` + +The `#[UseCheapestModel]` and `#[UseSmartestModel]` attributes are also available for automatic model selection. + +### Tools + +Implement the `HasTools` interface and scaffold tools with `php artisan make:tool`: + +```php +use Laravel\Ai\Contracts\HasTools; + +class MyAgent implements Agent, HasTools +{ + use Promptable; + + public function tools(): iterable + { + return [new MyCustomTool]; + } +} +``` + +### Provider Tools + +```php +use Laravel\Ai\Providers\Tools\{WebSearch, WebFetch, FileSearch}; + +public function tools(): iterable +{ + return [ + (new WebSearch)->max(5)->allow(['laravel.com']), + new WebFetch, + new FileSearch(stores: ['store_id']), + ]; +} +``` + +### Conversation Memory + +```php +use Laravel\Ai\Concerns\RemembersConversations; +use Laravel\Ai\Contracts\Conversational; + +class ChatBot implements Agent, Conversational +{ + use Promptable, RemembersConversations; + // ... +} + +$response = (new ChatBot)->forUser($user)->prompt('Hello!'); +$response = (new ChatBot)->continue($conversationId, as: $user)->prompt('More...'); +``` + +### Failover + +```php +$response = (new MyAgent)->prompt('Hello', provider: ['openai', 'anthropic']); +``` + +## Testing and Faking + +Each capability supports `fake()` with assertions: + +```php +use App\Ai\Agents\SalesCoach; +use Laravel\Ai\{Image, Audio, Transcription, Embeddings, Reranking, Files, Stores}; + +// Agents +SalesCoach::fake(['Response 1', 'Response 2']); +SalesCoach::assertPrompted('query'); +SalesCoach::assertNotPrompted('query'); +SalesCoach::assertNeverPrompted(); +SalesCoach::fake()->preventStrayPrompts(); + +// Images +Image::fake(); +Image::assertGenerated(fn ($prompt) => $prompt->contains('sunset')); +Image::assertNothingGenerated(); + +// Audio +Audio::fake(); +Audio::assertGenerated(fn ($prompt) => $prompt->contains('Hello')); + +// Transcription +Transcription::fake(['Transcribed text.']); +Transcription::assertGenerated(fn ($prompt) => $prompt->isDiarized()); + +// Embeddings +Embeddings::fake(); +Embeddings::assertGenerated(fn ($prompt) => $prompt->contains('Laravel')); + +// Reranking +Reranking::fake(); +Reranking::assertReranked(fn ($prompt) => $prompt->contains('PHP')); + +// Files +Files::fake(); +Files::assertStored(fn ($file) => $file->mimeType() === 'text/plain'); + +// Stores +Stores::fake(); +Stores::assertCreated('Knowledge Base'); +$store = Stores::get('id'); +$store->assertAdded('file_id'); +``` + +## Key Patterns + +- Namespace: `Laravel\Ai\` +- Package: `composer require laravel/ai` +- Agent pattern: Implement the `Agent` interface and use the `Promptable` trait +- Optional interfaces: `HasTools`, `HasMiddleware`, `HasStructuredOutput`, `Conversational` +- Entry-point classes: `Image`, `Audio`, `Transcription`, `Embeddings`, `Reranking`, `Stores` +- Artisan commands: `php artisan make:agent`, `php artisan make:tool` +- Global helper: `agent()` for anonymous agents + +## Common Pitfalls + +### Wrong Namespace + +The namespace is `Laravel\Ai`, not `Illuminate\Ai` or `Laravel\AI`. + +```php +// Correct +use Laravel\Ai\Image; +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Promptable; + +// Wrong — these do not exist +use Illuminate\Ai\Image; +use Laravel\AI\Agent; +``` + +### Unsupported Provider Capability + +Calling a capability not supported by a provider throws a `LogicException`. Refer to the provider support table below. + +### Never Use Prism Directly + +Use agents and entry-point classes (`Image`, `Audio`, etc.) — not `Prism::text()` directly. The AI SDK wraps Prism internally. + +## Provider Support + +| Provider | Text | Image | Audio | STT | Embeddings | Reranking | Files | Stores | +| ---------- | ---- | ----- | ----- | --- | ---------- | --------- | ----- | ------ | +| OpenAI | Y | Y | Y | Y | Y | - | Y | Y | +| Anthropic | Y | - | - | - | - | - | Y | - | +| Gemini | Y | Y | - | - | Y | - | Y | Y | +| xAI | Y | Y | - | - | - | - | - | - | +| Groq | Y | - | - | - | - | - | - | - | +| OpenRouter | Y | - | - | - | - | - | - | - | +| ElevenLabs | - | - | Y | Y | - | - | - | - | +| Cohere | - | - | - | - | Y | Y | - | - | +| Jina | - | - | - | - | Y | Y | - | - | \ No newline at end of file diff --git a/.agents/skills/tailwindcss-development/SKILL.md b/.agents/skills/tailwindcss-development/SKILL.md new file mode 100644 index 000000000..a8ae7a476 --- /dev/null +++ b/.agents/skills/tailwindcss-development/SKILL.md @@ -0,0 +1,100 @@ +--- +name: tailwindcss-development +description: "Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes." +license: MIT +metadata: + author: laravel +--- + +# Tailwind CSS Development + +## When to Apply + +Activate this skill when: +- Adding styles to components or pages +- Working with responsive design +- Implementing dark mode +- Extracting repeated patterns into components +- Debugging spacing or layout issues + +## Documentation + +Use `search-docs` for detailed Tailwind CSS v3 patterns and documentation. + +## Basic Usage + +- Use Tailwind CSS classes to style HTML. Check and follow existing Tailwind conventions in the project before introducing new patterns. +- Offer to extract repeated patterns into components that match the project's conventions (e.g., Blade, JSX, Vue). +- Consider class placement, order, priority, and defaults. Remove redundant classes, add classes to parent or child elements carefully to reduce repetition, and group elements logically. + +## Tailwind CSS v3 Specifics + +- Always use Tailwind CSS v3 and verify you're using only classes it supports. +- Configuration is done in the `tailwind.config.js` file. +- Import using `@tailwind` directives: + + +```css +@tailwind base; +@tailwind components; +@tailwind utilities; +``` + +## Spacing + +When listing items, use gap utilities for spacing; don't use margins. + + +```html +
+
Item 1
+
Item 2
+
+``` + +## Dark Mode + +If existing pages and components support dark mode, new pages and components must support it the same way, typically using the `dark:` variant: + + +```html +
+ Content adapts to color scheme +
+``` + +## Common Patterns + +### Flexbox Layout + + +```html +
+
Left content
+
Right content
+
+``` + +### Grid Layout + + +```html +
+
Card 1
+
Card 2
+
Card 3
+
+``` + +## Verification + +1. Check browser for visual rendering +2. Test responsive breakpoints +3. Verify dark mode if project uses it + +## Common Pitfalls + +- Using margins for spacing between siblings instead of gap utilities +- Forgetting to add dark mode variants when the project uses dark mode +- Not checking existing project conventions before adding new utilities +- Overusing inline styles when Tailwind classes would suffice \ No newline at end of file diff --git a/.env.example b/.env.example index bbfd24b95..4d5f3db8d 100644 --- a/.env.example +++ b/.env.example @@ -33,7 +33,7 @@ SESSION_ENCRYPT=false SESSION_PATH=/ SESSION_DOMAIN=null -BROADCAST_CONNECTION=log +BROADCAST_CONNECTION=reverb FILESYSTEM_DISK=s3 MEDIA_DISK=s3 LOCAL_MEDIA_DISK=public @@ -64,6 +64,20 @@ AWS_URL= AWS_ENDPOINT=https://hel1.your-objectstorage.com AWS_USE_PATH_STYLE_ENDPOINT=false +REVERB_APP_ID= +REVERB_APP_KEY= +REVERB_APP_SECRET= +REVERB_HOST=localhost +REVERB_PORT=8080 +REVERB_SCHEME=http +REVERB_SERVER_HOST=0.0.0.0 +REVERB_SERVER_PORT=8080 + +VITE_REVERB_APP_KEY="${REVERB_APP_KEY}" +VITE_REVERB_HOST="${REVERB_HOST}" +VITE_REVERB_PORT="${REVERB_PORT}" +VITE_REVERB_SCHEME="${REVERB_SCHEME}" + VITE_APP_NAME="${APP_NAME}" OPENAI_API_KEY= @@ -73,4 +87,4 @@ QUICK_LISTING_AI_MODEL=gpt-5.2 DEMO=0 -DEMO_TTL_MINUTES=360 \ No newline at end of file +DEMO_TTL_MINUTES=360 diff --git a/.github/skills/developing-with-ai-sdk/SKILL.md b/.github/skills/developing-with-ai-sdk/SKILL.md new file mode 100644 index 000000000..3e6c70300 --- /dev/null +++ b/.github/skills/developing-with-ai-sdk/SKILL.md @@ -0,0 +1,413 @@ +--- +name: developing-with-ai-sdk +description: Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter). +--- + +# Developing with the Laravel AI SDK + +The Laravel AI SDK (`laravel/ai`) is the official AI package for Laravel, providing a unified API for agents, images, audio, transcription, embeddings, reranking, vector stores, and file management across multiple AI providers. + +## Searching the Documentation + +This package is new. Always search the documentation before implementing any feature. Never guess at APIs — the documentation is the single source of truth. + +- Use broad, simple queries that match the documentation section headings below. +- Do not add package names to queries — package information is shared automatically. Use `test agent fake`, not `laravel ai test agent fake`. +- Run multiple queries at once — the most relevant results are returned first. + +### Documentation Sections + +Use these section headings as query terms for accurate results: + +- Introduction, Installation, Configuration, Provider Support +- Agents: Prompting, Conversation Context, Structured Output, Attachments, Streaming, Broadcasting, Queueing, Tools, Provider Tools, Middleware, Anonymous Agents, Agent Configuration +- Images +- Audio (TTS) +- Transcription (STT) +- Embeddings: Querying Embeddings, Caching Embeddings +- Reranking +- Files +- Vector Stores: Adding Files to Stores +- Failover +- Testing: Agents, Images, Audio, Transcriptions, Embeddings, Reranking, Files, Vector Stores +- Events + +## Decision Workflow + +Determine the right entry point before writing code: + +Text generation or chat? → Agent class with `Promptable` trait +Chat with conversation history? → Agent + `Conversational` interface (manual) or `RemembersConversations` trait (automatic) +Structured JSON output? → Agent + `HasStructuredOutput` interface +Image generation? → `Image::of()->generate()` +Audio synthesis? → `Audio::of()->generate()` +Transcription? → `Transcription::fromPath()->generate()` +Embeddings? → `Embeddings::for()->generate()` +Reranking? → `Reranking::of()->rerank()` +File storage? → `Document::fromPath()->put()` +Vector stores? → `Stores::create()` + +## Basic Usage Examples + +### Agents + +```php +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Promptable; + +class SalesCoach implements Agent +{ + use Promptable; + + public function instructions(): string + { + return 'You are a sales coach.'; + } +} + +// Prompting +$response = (new SalesCoach)->prompt('Analyze this transcript...'); +echo $response->text; + +// Streaming (returns SSE response from a route) +return (new SalesCoach)->stream('Analyze this transcript...'); + +// Queueing +(new SalesCoach)->queue('Analyze this transcript...') + ->then(fn ($response) => /* ... */); + +// Anonymous agents +use function Laravel\Ai\{agent}; + +$response = agent(instructions: 'You are a helpful assistant.')->prompt('Hello'); +``` + +### Conversation Context + +Manual conversation history via the `Conversational` interface: + +```php +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Contracts\Conversational; +use Laravel\Ai\Messages\Message; +use Laravel\Ai\Promptable; + +class SalesCoach implements Agent, Conversational +{ + use Promptable; + + public function __construct(public User $user) {} + + public function instructions(): string { return 'You are a sales coach.'; } + + public function messages(): iterable + { + return History::where('user_id', $this->user->id) + ->latest()->limit(50)->get()->reverse() + ->map(fn ($m) => new Message($m->role, $m->content)) + ->all(); + } +} +``` + +Automatic conversation persistence via the `RemembersConversations` trait: + +```php +use Laravel\Ai\Concerns\RemembersConversations; +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Contracts\Conversational; +use Laravel\Ai\Promptable; + +class SalesCoach implements Agent, Conversational +{ + use Promptable, RemembersConversations; + + public function instructions(): string { return 'You are a sales coach.'; } +} + +// Start a new conversation +$response = (new SalesCoach)->forUser($user)->prompt('Hello!'); +$conversationId = $response->conversationId; + +// Continue an existing conversation +$response = (new SalesCoach)->continue($conversationId, as: $user)->prompt('Tell me more.'); +``` + +### Structured Output + +```php +use Illuminate\Contracts\JsonSchema\JsonSchema; +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Contracts\HasStructuredOutput; +use Laravel\Ai\Promptable; + +class Reviewer implements Agent, HasStructuredOutput +{ + use Promptable; + + public function instructions(): string { return 'Review and score content.'; } + + public function schema(JsonSchema $schema): array + { + return [ + 'feedback' => $schema->string()->required(), + 'score' => $schema->integer()->min(1)->max(10)->required(), + ]; + } +} + +$response = (new Reviewer)->prompt('Review this...'); +echo $response['score']; // Access like an array +``` + +### Images + +```php +use Laravel\Ai\Image; + +$image = Image::of('A sunset over mountains') + ->landscape() + ->quality('high') + ->generate(); + +$path = $image->store(); // Store to default disk +``` + +### Audio + +```php +use Laravel\Ai\Audio; + +$audio = Audio::of('Hello from Laravel.') + ->female() + ->instructions('Speak warmly') + ->generate(); + +$path = $audio->store(); +``` + +### Transcription + +```php +use Laravel\Ai\Transcription; + +$transcript = Transcription::fromStorage('audio.mp3') + ->diarize() + ->generate(); + +echo (string) $transcript; +``` + +### Embeddings + +```php +use Laravel\Ai\Embeddings; +use Illuminate\Support\Str; + +$response = Embeddings::for(['Text one', 'Text two']) + ->dimensions(1536) + ->cache() + ->generate(); + +// Single string via Stringable +$embedding = Str::of('Napa Valley has great wine.')->toEmbeddings(); +``` + +### Reranking + +```php +use Laravel\Ai\Reranking; + +$response = Reranking::of(['Django is Python.', 'Laravel is PHP.', 'React is JS.']) + ->limit(5) + ->rerank('PHP frameworks'); + +$response->first()->document; // "Laravel is PHP." +``` + +### Files and Vector Stores + +```php +use Laravel\Ai\Files\Document; +use Laravel\Ai\Stores; + +// Store a file with the provider +$file = Document::fromPath('/path/to/doc.pdf')->put(); + +// Create a vector store and add files +$store = Stores::create('Knowledge Base'); +$store->add($file->id); +$store->add(Document::fromStorage('manual.pdf')); // Store + add in one step +``` + +## Agent Configuration + +### PHP Attributes + +```php +use Laravel\Ai\Attributes\{Provider, MaxSteps, MaxTokens, Temperature, Timeout}; + +#[Provider('anthropic')] +#[MaxSteps(10)] +#[MaxTokens(4096)] +#[Temperature(0.7)] +#[Timeout(120)] +class MyAgent implements Agent +{ + use Promptable; + // ... +} +``` + +The `#[UseCheapestModel]` and `#[UseSmartestModel]` attributes are also available for automatic model selection. + +### Tools + +Implement the `HasTools` interface and scaffold tools with `php artisan make:tool`: + +```php +use Laravel\Ai\Contracts\HasTools; + +class MyAgent implements Agent, HasTools +{ + use Promptable; + + public function tools(): iterable + { + return [new MyCustomTool]; + } +} +``` + +### Provider Tools + +```php +use Laravel\Ai\Providers\Tools\{WebSearch, WebFetch, FileSearch}; + +public function tools(): iterable +{ + return [ + (new WebSearch)->max(5)->allow(['laravel.com']), + new WebFetch, + new FileSearch(stores: ['store_id']), + ]; +} +``` + +### Conversation Memory + +```php +use Laravel\Ai\Concerns\RemembersConversations; +use Laravel\Ai\Contracts\Conversational; + +class ChatBot implements Agent, Conversational +{ + use Promptable, RemembersConversations; + // ... +} + +$response = (new ChatBot)->forUser($user)->prompt('Hello!'); +$response = (new ChatBot)->continue($conversationId, as: $user)->prompt('More...'); +``` + +### Failover + +```php +$response = (new MyAgent)->prompt('Hello', provider: ['openai', 'anthropic']); +``` + +## Testing and Faking + +Each capability supports `fake()` with assertions: + +```php +use App\Ai\Agents\SalesCoach; +use Laravel\Ai\{Image, Audio, Transcription, Embeddings, Reranking, Files, Stores}; + +// Agents +SalesCoach::fake(['Response 1', 'Response 2']); +SalesCoach::assertPrompted('query'); +SalesCoach::assertNotPrompted('query'); +SalesCoach::assertNeverPrompted(); +SalesCoach::fake()->preventStrayPrompts(); + +// Images +Image::fake(); +Image::assertGenerated(fn ($prompt) => $prompt->contains('sunset')); +Image::assertNothingGenerated(); + +// Audio +Audio::fake(); +Audio::assertGenerated(fn ($prompt) => $prompt->contains('Hello')); + +// Transcription +Transcription::fake(['Transcribed text.']); +Transcription::assertGenerated(fn ($prompt) => $prompt->isDiarized()); + +// Embeddings +Embeddings::fake(); +Embeddings::assertGenerated(fn ($prompt) => $prompt->contains('Laravel')); + +// Reranking +Reranking::fake(); +Reranking::assertReranked(fn ($prompt) => $prompt->contains('PHP')); + +// Files +Files::fake(); +Files::assertStored(fn ($file) => $file->mimeType() === 'text/plain'); + +// Stores +Stores::fake(); +Stores::assertCreated('Knowledge Base'); +$store = Stores::get('id'); +$store->assertAdded('file_id'); +``` + +## Key Patterns + +- Namespace: `Laravel\Ai\` +- Package: `composer require laravel/ai` +- Agent pattern: Implement the `Agent` interface and use the `Promptable` trait +- Optional interfaces: `HasTools`, `HasMiddleware`, `HasStructuredOutput`, `Conversational` +- Entry-point classes: `Image`, `Audio`, `Transcription`, `Embeddings`, `Reranking`, `Stores` +- Artisan commands: `php artisan make:agent`, `php artisan make:tool` +- Global helper: `agent()` for anonymous agents + +## Common Pitfalls + +### Wrong Namespace + +The namespace is `Laravel\Ai`, not `Illuminate\Ai` or `Laravel\AI`. + +```php +// Correct +use Laravel\Ai\Image; +use Laravel\Ai\Contracts\Agent; +use Laravel\Ai\Promptable; + +// Wrong — these do not exist +use Illuminate\Ai\Image; +use Laravel\AI\Agent; +``` + +### Unsupported Provider Capability + +Calling a capability not supported by a provider throws a `LogicException`. Refer to the provider support table below. + +### Never Use Prism Directly + +Use agents and entry-point classes (`Image`, `Audio`, etc.) — not `Prism::text()` directly. The AI SDK wraps Prism internally. + +## Provider Support + +| Provider | Text | Image | Audio | STT | Embeddings | Reranking | Files | Stores | +| ---------- | ---- | ----- | ----- | --- | ---------- | --------- | ----- | ------ | +| OpenAI | Y | Y | Y | Y | Y | - | Y | Y | +| Anthropic | Y | - | - | - | - | - | Y | - | +| Gemini | Y | Y | - | - | Y | - | Y | Y | +| xAI | Y | Y | - | - | - | - | - | - | +| Groq | Y | - | - | - | - | - | - | - | +| OpenRouter | Y | - | - | - | - | - | - | - | +| ElevenLabs | - | - | Y | Y | - | - | - | - | +| Cohere | - | - | - | - | Y | Y | - | - | +| Jina | - | - | - | - | Y | Y | - | - | \ No newline at end of file diff --git a/.github/skills/tailwindcss-development/SKILL.md b/.github/skills/tailwindcss-development/SKILL.md new file mode 100644 index 000000000..a8ae7a476 --- /dev/null +++ b/.github/skills/tailwindcss-development/SKILL.md @@ -0,0 +1,100 @@ +--- +name: tailwindcss-development +description: "Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes." +license: MIT +metadata: + author: laravel +--- + +# Tailwind CSS Development + +## When to Apply + +Activate this skill when: +- Adding styles to components or pages +- Working with responsive design +- Implementing dark mode +- Extracting repeated patterns into components +- Debugging spacing or layout issues + +## Documentation + +Use `search-docs` for detailed Tailwind CSS v3 patterns and documentation. + +## Basic Usage + +- Use Tailwind CSS classes to style HTML. Check and follow existing Tailwind conventions in the project before introducing new patterns. +- Offer to extract repeated patterns into components that match the project's conventions (e.g., Blade, JSX, Vue). +- Consider class placement, order, priority, and defaults. Remove redundant classes, add classes to parent or child elements carefully to reduce repetition, and group elements logically. + +## Tailwind CSS v3 Specifics + +- Always use Tailwind CSS v3 and verify you're using only classes it supports. +- Configuration is done in the `tailwind.config.js` file. +- Import using `@tailwind` directives: + + +```css +@tailwind base; +@tailwind components; +@tailwind utilities; +``` + +## Spacing + +When listing items, use gap utilities for spacing; don't use margins. + + +```html +
+
Item 1
+
Item 2
+
+``` + +## Dark Mode + +If existing pages and components support dark mode, new pages and components must support it the same way, typically using the `dark:` variant: + + +```html +
+ Content adapts to color scheme +
+``` + +## Common Patterns + +### Flexbox Layout + + +```html +
+
Left content
+
Right content
+
+``` + +### Grid Layout + + +```html +
+
Card 1
+
Card 2
+
Card 3
+
+``` + +## Verification + +1. Check browser for visual rendering +2. Test responsive breakpoints +3. Verify dark mode if project uses it + +## Common Pitfalls + +- Using margins for spacing between siblings instead of gap utilities +- Forgetting to add dark mode variants when the project uses dark mode +- Not checking existing project conventions before adding new utilities +- Overusing inline styles when Tailwind classes would suffice \ No newline at end of file diff --git a/AGENTS.md b/AGENTS.md index 031b84595..626e2f1e2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,3 +5,418 @@ Act as a Senior Laravel & FilamentPHP Architect. Refactor the attached code as a 4. Database: Consolidate migrations into a single file per table or topic (e.g., users, cache, jobs) to reduce the overall number of migration files. 5. Modularity: Use the `laravel-modules` package to encapsulate all features, routing, and Filament resources strictly inside their respective modules. 6. Frontend: Optimize and reduce the CSS footprint while maintaining the exact same visual output. + +=== + + +=== foundation rules === + +# Laravel Boost Guidelines + +The Laravel Boost guidelines are specifically curated by Laravel maintainers for this application. These guidelines should be followed closely to ensure the best experience when building Laravel applications. + +## Foundational Context + +This application is a Laravel application and its main Laravel ecosystems package & versions are below. You are an expert with them all. Ensure you abide by these specific packages & versions. + +- php - 8.4.18 +- filament/filament (FILAMENT) - v5 +- laravel/ai (AI) - v0 +- laravel/framework (LARAVEL) - v12 +- laravel/prompts (PROMPTS) - v0 +- laravel/reverb (REVERB) - v1 +- laravel/sanctum (SANCTUM) - v4 +- laravel/socialite (SOCIALITE) - v5 +- livewire/livewire (LIVEWIRE) - v4 +- laravel/boost (BOOST) - v2 +- laravel/mcp (MCP) - v0 +- laravel/pail (PAIL) - v1 +- laravel/pint (PINT) - v1 +- laravel/sail (SAIL) - v1 +- phpunit/phpunit (PHPUNIT) - v11 +- alpinejs (ALPINEJS) - v3 +- laravel-echo (ECHO) - v2 +- tailwindcss (TAILWINDCSS) - v3 + +## Skills Activation + +This project has domain-specific skills available. You MUST activate the relevant skill whenever you work in that domain—don't wait until you're stuck. + +- `tailwindcss-development` — Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes. +- `developing-with-ai-sdk` — Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter). + +## Conventions + +- You must follow all existing code conventions used in this application. When creating or editing a file, check sibling files for the correct structure, approach, and naming. +- Use descriptive names for variables and methods. For example, `isRegisteredForDiscounts`, not `discount()`. +- Check for existing components to reuse before writing a new one. + +## Verification Scripts + +- Do not create verification scripts or tinker when tests cover that functionality and prove they work. Unit and feature tests are more important. + +## Application Structure & Architecture + +- Stick to existing directory structure; don't create new base folders without approval. +- Do not change the application's dependencies without approval. + +## Frontend Bundling + +- If the user doesn't see a frontend change reflected in the UI, it could mean they need to run `npm run build`, `npm run dev`, or `composer run dev`. Ask them. + +## Documentation Files + +- You must only create documentation files if explicitly requested by the user. + +## Replies + +- Be concise in your explanations - focus on what's important rather than explaining obvious details. + +=== boost rules === + +# Laravel Boost + +- Laravel Boost is an MCP server that comes with powerful tools designed specifically for this application. Use them. + +## Artisan + +- Use the `list-artisan-commands` tool when you need to call an Artisan command to double-check the available parameters. + +## URLs + +- Whenever you share a project URL with the user, you should use the `get-absolute-url` tool to ensure you're using the correct scheme, domain/IP, and port. + +## Tinker / Debugging + +- You should use the `tinker` tool when you need to execute PHP to debug code or query Eloquent models directly. +- Use the `database-query` tool when you only need to read from the database. +- Use the `database-schema` tool to inspect table structure before writing migrations or models. + +## Reading Browser Logs With the `browser-logs` Tool + +- You can read browser logs, errors, and exceptions using the `browser-logs` tool from Boost. +- Only recent browser logs will be useful - ignore old logs. + +## Searching Documentation (Critically Important) + +- Boost comes with a powerful `search-docs` tool you should use before trying other approaches when working with Laravel or Laravel ecosystem packages. This tool automatically passes a list of installed packages and their versions to the remote Boost API, so it returns only version-specific documentation for the user's circumstance. You should pass an array of packages to filter on if you know you need docs for particular packages. +- Search the documentation before making code changes to ensure we are taking the correct approach. +- Use multiple, broad, simple, topic-based queries at once. For example: `['rate limiting', 'routing rate limiting', 'routing']`. The most relevant results will be returned first. +- Do not add package names to queries; package information is already shared. For example, use `test resource table`, not `filament 4 test resource table`. + +### Available Search Syntax + +1. Simple Word Searches with auto-stemming - query=authentication - finds 'authenticate' and 'auth'. +2. Multiple Words (AND Logic) - query=rate limit - finds knowledge containing both "rate" AND "limit". +3. Quoted Phrases (Exact Position) - query="infinite scroll" - words must be adjacent and in that order. +4. Mixed Queries - query=middleware "rate limit" - "middleware" AND exact phrase "rate limit". +5. Multiple Queries - queries=["authentication", "middleware"] - ANY of these terms. + +=== php rules === + +# PHP + +- Always use curly braces for control structures, even for single-line bodies. + +## Constructors + +- Use PHP 8 constructor property promotion in `__construct()`. + - `public function __construct(public GitHub $github) { }` +- Do not allow empty `__construct()` methods with zero parameters unless the constructor is private. + +## Type Declarations + +- Always use explicit return type declarations for methods and functions. +- Use appropriate PHP type hints for method parameters. + + +```php +protected function isAccessible(User $user, ?string $path = null): bool +{ + ... +} +``` + +## Enums + +- Typically, keys in an Enum should be TitleCase. For example: `FavoritePerson`, `BestLake`, `Monthly`. + +## Comments + +- Prefer PHPDoc blocks over inline comments. Never use comments within the code itself unless the logic is exceptionally complex. + +## PHPDoc Blocks + +- Add useful array shape type definitions when appropriate. + +=== laravel/core rules === + +# Do Things the Laravel Way + +- Use `php artisan make:` commands to create new files (i.e. migrations, controllers, models, etc.). You can list available Artisan commands using the `list-artisan-commands` tool. +- If you're creating a generic PHP class, use `php artisan make:class`. +- Pass `--no-interaction` to all Artisan commands to ensure they work without user input. You should also pass the correct `--options` to ensure correct behavior. + +## Database + +- Always use proper Eloquent relationship methods with return type hints. Prefer relationship methods over raw queries or manual joins. +- Use Eloquent models and relationships before suggesting raw database queries. +- Avoid `DB::`; prefer `Model::query()`. Generate code that leverages Laravel's ORM capabilities rather than bypassing them. +- Generate code that prevents N+1 query problems by using eager loading. +- Use Laravel's query builder for very complex database operations. + +### Model Creation + +- When creating new models, create useful factories and seeders for them too. Ask the user if they need any other things, using `list-artisan-commands` to check the available options to `php artisan make:model`. + +### APIs & Eloquent Resources + +- For APIs, default to using Eloquent API Resources and API versioning unless existing API routes do not, then you should follow existing application convention. + +## Controllers & Validation + +- Always create Form Request classes for validation rather than inline validation in controllers. Include both validation rules and custom error messages. +- Check sibling Form Requests to see if the application uses array or string based validation rules. + +## Authentication & Authorization + +- Use Laravel's built-in authentication and authorization features (gates, policies, Sanctum, etc.). + +## URL Generation + +- When generating links to other pages, prefer named routes and the `route()` function. + +## Queues + +- Use queued jobs for time-consuming operations with the `ShouldQueue` interface. + +## Configuration + +- Use environment variables only in configuration files - never use the `env()` function directly outside of config files. Always use `config('app.name')`, not `env('APP_NAME')`. + +## Testing + +- When creating models for tests, use the factories for the models. Check if the factory has custom states that can be used before manually setting up the model. +- Faker: Use methods such as `$this->faker->word()` or `fake()->randomDigit()`. Follow existing conventions whether to use `$this->faker` or `fake()`. +- When creating tests, make use of `php artisan make:test [options] {name}` to create a feature test, and pass `--unit` to create a unit test. Most tests should be feature tests. + +## Vite Error + +- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file in Vite manifest" error, you can run `npm run build` or ask the user to run `npm run dev` or `composer run dev`. + +=== laravel/v12 rules === + +# Laravel 12 + +- CRITICAL: ALWAYS use `search-docs` tool for version-specific Laravel documentation and updated code examples. +- Since Laravel 11, Laravel has a new streamlined file structure which this project uses. + +## Laravel 12 Structure + +- In Laravel 12, middleware are no longer registered in `app/Http/Kernel.php`. +- Middleware are configured declaratively in `bootstrap/app.php` using `Application::configure()->withMiddleware()`. +- `bootstrap/app.php` is the file to register middleware, exceptions, and routing files. +- `bootstrap/providers.php` contains application specific service providers. +- The `app\Console\Kernel.php` file no longer exists; use `bootstrap/app.php` or `routes/console.php` for console configuration. +- Console commands in `app/Console/Commands/` are automatically available and do not require manual registration. + +## Database + +- When modifying a column, the migration must include all of the attributes that were previously defined on the column. Otherwise, they will be dropped and lost. +- Laravel 12 allows limiting eagerly loaded records natively, without external packages: `$query->latest()->limit(10);`. + +### Models + +- Casts can and likely should be set in a `casts()` method on a model rather than the `$casts` property. Follow existing conventions from other models. + +=== pint/core rules === + +# Laravel Pint Code Formatter + +- If you have modified any PHP files, you must run `vendor/bin/pint --dirty --format agent` before finalizing changes to ensure your code matches the project's expected style. +- Do not run `vendor/bin/pint --test --format agent`, simply run `vendor/bin/pint --format agent` to fix any formatting issues. + +=== phpunit/core rules === + +# PHPUnit + +- This application uses PHPUnit for testing. All tests must be written as PHPUnit classes. Use `php artisan make:test --phpunit {name}` to create a new test. +- If you see a test using "Pest", convert it to PHPUnit. +- Every time a test has been updated, run that singular test. +- When the tests relating to your feature are passing, ask the user if they would like to also run the entire test suite to make sure everything is still passing. +- Tests should cover all happy paths, failure paths, and edge cases. +- You must not remove any tests or test files from the tests directory without approval. These are not temporary or helper files; these are core to the application. + +## Running Tests + +- Run the minimal number of tests, using an appropriate filter, before finalizing. +- To run all tests: `php artisan test --compact`. +- To run all tests in a file: `php artisan test --compact tests/Feature/ExampleTest.php`. +- To filter on a particular test name: `php artisan test --compact --filter=testName` (recommended after making a change to a related file). + +=== tailwindcss/core rules === + +# Tailwind CSS + +- Always use existing Tailwind conventions; check project patterns before adding new ones. +- IMPORTANT: Always use `search-docs` tool for version-specific Tailwind CSS documentation and updated code examples. Never rely on training data. +- IMPORTANT: Activate `tailwindcss-development` every time you're working with a Tailwind CSS or styling-related task. + +=== filament/filament rules === + +## Filament + +- Filament is used by this application. Follow the existing conventions for how and where it is implemented. +- Filament is a Server-Driven UI (SDUI) framework for Laravel that lets you define user interfaces in PHP using structured configuration objects. Built on Livewire, Alpine.js, and Tailwind CSS. +- Use the `search-docs` tool for official documentation on Artisan commands, code examples, testing, relationships, and idiomatic practices. If `search-docs` is unavailable, refer to https://filamentphp.com/docs. + +### Artisan + +- Always use Filament-specific Artisan commands to create files. Find available commands with the `list-artisan-commands` tool, or run `php artisan --help`. +- Always inspect required options before running a command, and always pass `--no-interaction`. + +### Patterns + +Always use static `make()` methods to initialize components. Most configuration methods accept a `Closure` for dynamic values. + +Use `Get $get` to read other form field values for conditional logic: + + +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; +use Filament\Schemas\Components\Utilities\Get; + +Select::make('type') + ->options(CompanyType::class) + ->required() + ->live(), + +TextInput::make('company_name') + ->required() + ->visible(fn (Get $get): bool => $get('type') === 'business'), + + + +Use `state()` with a `Closure` to compute derived column values: + + +use Filament\Tables\Columns\TextColumn; + +TextColumn::make('full_name') + ->state(fn (User $record): string => "{$record->first_name} {$record->last_name}"), + + + +Actions encapsulate a button with an optional modal form and logic: + + +use Filament\Actions\Action; +use Filament\Forms\Components\TextInput; + +Action::make('updateEmail') + ->schema([ + TextInput::make('email') + ->email() + ->required(), + ]) + ->action(fn (array $data, User $record) => $record->update($data)) + + + +### Testing + +Always authenticate before testing panel functionality. Filament uses Livewire, so use `Livewire::test()` or `livewire()` (available when `pestphp/pest-plugin-livewire` is in `composer.json`): + + +use function Pest\Livewire\livewire; + +livewire(ListUsers::class) + ->assertCanSeeTableRecords($users) + ->searchTable($users->first()->name) + ->assertCanSeeTableRecords($users->take(1)) + ->assertCanNotSeeTableRecords($users->skip(1)); + + + + +use function Pest\Laravel\assertDatabaseHas; +use function Pest\Livewire\livewire; + +livewire(CreateUser::class) + ->fillForm([ + 'name' => 'Test', + 'email' => 'test@example.com', + ]) + ->call('create') + ->assertNotified() + ->assertRedirect(); + +assertDatabaseHas(User::class, [ + 'name' => 'Test', + 'email' => 'test@example.com', +]); + + + + +use function Pest\Livewire\livewire; + +livewire(CreateUser::class) + ->fillForm([ + 'name' => null, + 'email' => 'invalid-email', + ]) + ->call('create') + ->assertHasFormErrors([ + 'name' => 'required', + 'email' => 'email', + ]) + ->assertNotNotified(); + + + + +use Filament\Actions\DeleteAction; +use function Pest\Livewire\livewire; + +livewire(EditUser::class, ['record' => $user->id]) + ->callAction(DeleteAction::class) + ->assertNotified() + ->assertRedirect(); + + + + +use Filament\Actions\Testing\TestAction; +use function Pest\Livewire\livewire; + +livewire(ListUsers::class) + ->callAction(TestAction::make('promote')->table($user), [ + 'role' => 'admin', + ]) + ->assertNotified(); + + + +### Correct Namespaces + +- Form fields (`TextInput`, `Select`, etc.): `Filament\Forms\Components\` +- Infolist entries (`TextEntry`, `IconEntry`, etc.): `Filament\Infolists\Components\` +- Layout components (`Grid`, `Section`, `Fieldset`, `Tabs`, `Wizard`, etc.): `Filament\Schemas\Components\` +- Schema utilities (`Get`, `Set`, etc.): `Filament\Schemas\Components\Utilities\` +- Actions (`DeleteAction`, `CreateAction`, etc.): `Filament\Actions\`. Never use `Filament\Tables\Actions\`, `Filament\Forms\Actions\`, or any other sub-namespace for actions. +- Icons: `Filament\Support\Icons\Heroicon` enum (e.g., `Heroicon::PencilSquare`) + +### Common Mistakes + +- **Never assume public file visibility.** File visibility is `private` by default. Always use `->visibility('public')` when public access is needed. +- **Never assume full-width layout.** `Grid`, `Section`, and `Fieldset` do not span all columns by default. Explicitly set column spans when needed. + +=== laravel/ai rules === + +## Laravel AI SDK + +- This application uses the Laravel AI SDK (`laravel/ai`) for all AI functionality. +- Activate the `developing-with-ai-sdk` skill when building, editing, updating, debugging, or testing AI agents, text generation, chat, streaming, structured output, tools, image generation, audio, transcription, embeddings, reranking, vector stores, files, conversation memory, or any AI provider integration (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter). + + diff --git a/GEMINI.md b/GEMINI.md new file mode 100644 index 000000000..0b86a9f57 --- /dev/null +++ b/GEMINI.md @@ -0,0 +1,412 @@ + +=== foundation rules === + +# Laravel Boost Guidelines + +The Laravel Boost guidelines are specifically curated by Laravel maintainers for this application. These guidelines should be followed closely to ensure the best experience when building Laravel applications. + +## Foundational Context + +This application is a Laravel application and its main Laravel ecosystems package & versions are below. You are an expert with them all. Ensure you abide by these specific packages & versions. + +- php - 8.4.18 +- filament/filament (FILAMENT) - v5 +- laravel/ai (AI) - v0 +- laravel/framework (LARAVEL) - v12 +- laravel/prompts (PROMPTS) - v0 +- laravel/reverb (REVERB) - v1 +- laravel/sanctum (SANCTUM) - v4 +- laravel/socialite (SOCIALITE) - v5 +- livewire/livewire (LIVEWIRE) - v4 +- laravel/boost (BOOST) - v2 +- laravel/mcp (MCP) - v0 +- laravel/pail (PAIL) - v1 +- laravel/pint (PINT) - v1 +- laravel/sail (SAIL) - v1 +- phpunit/phpunit (PHPUNIT) - v11 +- alpinejs (ALPINEJS) - v3 +- laravel-echo (ECHO) - v2 +- tailwindcss (TAILWINDCSS) - v3 + +## Skills Activation + +This project has domain-specific skills available. You MUST activate the relevant skill whenever you work in that domain—don't wait until you're stuck. + +- `tailwindcss-development` — Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes. +- `developing-with-ai-sdk` — Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter). + +## Conventions + +- You must follow all existing code conventions used in this application. When creating or editing a file, check sibling files for the correct structure, approach, and naming. +- Use descriptive names for variables and methods. For example, `isRegisteredForDiscounts`, not `discount()`. +- Check for existing components to reuse before writing a new one. + +## Verification Scripts + +- Do not create verification scripts or tinker when tests cover that functionality and prove they work. Unit and feature tests are more important. + +## Application Structure & Architecture + +- Stick to existing directory structure; don't create new base folders without approval. +- Do not change the application's dependencies without approval. + +## Frontend Bundling + +- If the user doesn't see a frontend change reflected in the UI, it could mean they need to run `npm run build`, `npm run dev`, or `composer run dev`. Ask them. + +## Documentation Files + +- You must only create documentation files if explicitly requested by the user. + +## Replies + +- Be concise in your explanations - focus on what's important rather than explaining obvious details. + +=== boost rules === + +# Laravel Boost + +- Laravel Boost is an MCP server that comes with powerful tools designed specifically for this application. Use them. + +## Artisan + +- Use the `list-artisan-commands` tool when you need to call an Artisan command to double-check the available parameters. + +## URLs + +- Whenever you share a project URL with the user, you should use the `get-absolute-url` tool to ensure you're using the correct scheme, domain/IP, and port. + +## Tinker / Debugging + +- You should use the `tinker` tool when you need to execute PHP to debug code or query Eloquent models directly. +- Use the `database-query` tool when you only need to read from the database. +- Use the `database-schema` tool to inspect table structure before writing migrations or models. + +## Reading Browser Logs With the `browser-logs` Tool + +- You can read browser logs, errors, and exceptions using the `browser-logs` tool from Boost. +- Only recent browser logs will be useful - ignore old logs. + +## Searching Documentation (Critically Important) + +- Boost comes with a powerful `search-docs` tool you should use before trying other approaches when working with Laravel or Laravel ecosystem packages. This tool automatically passes a list of installed packages and their versions to the remote Boost API, so it returns only version-specific documentation for the user's circumstance. You should pass an array of packages to filter on if you know you need docs for particular packages. +- Search the documentation before making code changes to ensure we are taking the correct approach. +- Use multiple, broad, simple, topic-based queries at once. For example: `['rate limiting', 'routing rate limiting', 'routing']`. The most relevant results will be returned first. +- Do not add package names to queries; package information is already shared. For example, use `test resource table`, not `filament 4 test resource table`. + +### Available Search Syntax + +1. Simple Word Searches with auto-stemming - query=authentication - finds 'authenticate' and 'auth'. +2. Multiple Words (AND Logic) - query=rate limit - finds knowledge containing both "rate" AND "limit". +3. Quoted Phrases (Exact Position) - query="infinite scroll" - words must be adjacent and in that order. +4. Mixed Queries - query=middleware "rate limit" - "middleware" AND exact phrase "rate limit". +5. Multiple Queries - queries=["authentication", "middleware"] - ANY of these terms. + +=== php rules === + +# PHP + +- Always use curly braces for control structures, even for single-line bodies. + +## Constructors + +- Use PHP 8 constructor property promotion in `__construct()`. + - `public function __construct(public GitHub $github) { }` +- Do not allow empty `__construct()` methods with zero parameters unless the constructor is private. + +## Type Declarations + +- Always use explicit return type declarations for methods and functions. +- Use appropriate PHP type hints for method parameters. + + +```php +protected function isAccessible(User $user, ?string $path = null): bool +{ + ... +} +``` + +## Enums + +- Typically, keys in an Enum should be TitleCase. For example: `FavoritePerson`, `BestLake`, `Monthly`. + +## Comments + +- Prefer PHPDoc blocks over inline comments. Never use comments within the code itself unless the logic is exceptionally complex. + +## PHPDoc Blocks + +- Add useful array shape type definitions when appropriate. + +=== laravel/core rules === + +# Do Things the Laravel Way + +- Use `php artisan make:` commands to create new files (i.e. migrations, controllers, models, etc.). You can list available Artisan commands using the `list-artisan-commands` tool. +- If you're creating a generic PHP class, use `php artisan make:class`. +- Pass `--no-interaction` to all Artisan commands to ensure they work without user input. You should also pass the correct `--options` to ensure correct behavior. + +## Database + +- Always use proper Eloquent relationship methods with return type hints. Prefer relationship methods over raw queries or manual joins. +- Use Eloquent models and relationships before suggesting raw database queries. +- Avoid `DB::`; prefer `Model::query()`. Generate code that leverages Laravel's ORM capabilities rather than bypassing them. +- Generate code that prevents N+1 query problems by using eager loading. +- Use Laravel's query builder for very complex database operations. + +### Model Creation + +- When creating new models, create useful factories and seeders for them too. Ask the user if they need any other things, using `list-artisan-commands` to check the available options to `php artisan make:model`. + +### APIs & Eloquent Resources + +- For APIs, default to using Eloquent API Resources and API versioning unless existing API routes do not, then you should follow existing application convention. + +## Controllers & Validation + +- Always create Form Request classes for validation rather than inline validation in controllers. Include both validation rules and custom error messages. +- Check sibling Form Requests to see if the application uses array or string based validation rules. + +## Authentication & Authorization + +- Use Laravel's built-in authentication and authorization features (gates, policies, Sanctum, etc.). + +## URL Generation + +- When generating links to other pages, prefer named routes and the `route()` function. + +## Queues + +- Use queued jobs for time-consuming operations with the `ShouldQueue` interface. + +## Configuration + +- Use environment variables only in configuration files - never use the `env()` function directly outside of config files. Always use `config('app.name')`, not `env('APP_NAME')`. + +## Testing + +- When creating models for tests, use the factories for the models. Check if the factory has custom states that can be used before manually setting up the model. +- Faker: Use methods such as `$this->faker->word()` or `fake()->randomDigit()`. Follow existing conventions whether to use `$this->faker` or `fake()`. +- When creating tests, make use of `php artisan make:test [options] {name}` to create a feature test, and pass `--unit` to create a unit test. Most tests should be feature tests. + +## Vite Error + +- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file in Vite manifest" error, you can run `npm run build` or ask the user to run `npm run dev` or `composer run dev`. + +=== laravel/v12 rules === + +# Laravel 12 + +- CRITICAL: ALWAYS use `search-docs` tool for version-specific Laravel documentation and updated code examples. +- Since Laravel 11, Laravel has a new streamlined file structure which this project uses. + +## Laravel 12 Structure + +- In Laravel 12, middleware are no longer registered in `app/Http/Kernel.php`. +- Middleware are configured declaratively in `bootstrap/app.php` using `Application::configure()->withMiddleware()`. +- `bootstrap/app.php` is the file to register middleware, exceptions, and routing files. +- `bootstrap/providers.php` contains application specific service providers. +- The `app\Console\Kernel.php` file no longer exists; use `bootstrap/app.php` or `routes/console.php` for console configuration. +- Console commands in `app/Console/Commands/` are automatically available and do not require manual registration. + +## Database + +- When modifying a column, the migration must include all of the attributes that were previously defined on the column. Otherwise, they will be dropped and lost. +- Laravel 12 allows limiting eagerly loaded records natively, without external packages: `$query->latest()->limit(10);`. + +### Models + +- Casts can and likely should be set in a `casts()` method on a model rather than the `$casts` property. Follow existing conventions from other models. + +=== pint/core rules === + +# Laravel Pint Code Formatter + +- If you have modified any PHP files, you must run `vendor/bin/pint --dirty --format agent` before finalizing changes to ensure your code matches the project's expected style. +- Do not run `vendor/bin/pint --test --format agent`, simply run `vendor/bin/pint --format agent` to fix any formatting issues. + +=== phpunit/core rules === + +# PHPUnit + +- This application uses PHPUnit for testing. All tests must be written as PHPUnit classes. Use `php artisan make:test --phpunit {name}` to create a new test. +- If you see a test using "Pest", convert it to PHPUnit. +- Every time a test has been updated, run that singular test. +- When the tests relating to your feature are passing, ask the user if they would like to also run the entire test suite to make sure everything is still passing. +- Tests should cover all happy paths, failure paths, and edge cases. +- You must not remove any tests or test files from the tests directory without approval. These are not temporary or helper files; these are core to the application. + +## Running Tests + +- Run the minimal number of tests, using an appropriate filter, before finalizing. +- To run all tests: `php artisan test --compact`. +- To run all tests in a file: `php artisan test --compact tests/Feature/ExampleTest.php`. +- To filter on a particular test name: `php artisan test --compact --filter=testName` (recommended after making a change to a related file). + +=== tailwindcss/core rules === + +# Tailwind CSS + +- Always use existing Tailwind conventions; check project patterns before adding new ones. +- IMPORTANT: Always use `search-docs` tool for version-specific Tailwind CSS documentation and updated code examples. Never rely on training data. +- IMPORTANT: Activate `tailwindcss-development` every time you're working with a Tailwind CSS or styling-related task. + +=== filament/filament rules === + +## Filament + +- Filament is used by this application. Follow the existing conventions for how and where it is implemented. +- Filament is a Server-Driven UI (SDUI) framework for Laravel that lets you define user interfaces in PHP using structured configuration objects. Built on Livewire, Alpine.js, and Tailwind CSS. +- Use the `search-docs` tool for official documentation on Artisan commands, code examples, testing, relationships, and idiomatic practices. If `search-docs` is unavailable, refer to https://filamentphp.com/docs. + +### Artisan + +- Always use Filament-specific Artisan commands to create files. Find available commands with the `list-artisan-commands` tool, or run `php artisan --help`. +- Always inspect required options before running a command, and always pass `--no-interaction`. + +### Patterns + +Always use static `make()` methods to initialize components. Most configuration methods accept a `Closure` for dynamic values. + +Use `Get $get` to read other form field values for conditional logic: + + +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; +use Filament\Schemas\Components\Utilities\Get; + +Select::make('type') + ->options(CompanyType::class) + ->required() + ->live(), + +TextInput::make('company_name') + ->required() + ->visible(fn (Get $get): bool => $get('type') === 'business'), + + + +Use `state()` with a `Closure` to compute derived column values: + + +use Filament\Tables\Columns\TextColumn; + +TextColumn::make('full_name') + ->state(fn (User $record): string => "{$record->first_name} {$record->last_name}"), + + + +Actions encapsulate a button with an optional modal form and logic: + + +use Filament\Actions\Action; +use Filament\Forms\Components\TextInput; + +Action::make('updateEmail') + ->schema([ + TextInput::make('email') + ->email() + ->required(), + ]) + ->action(fn (array $data, User $record) => $record->update($data)) + + + +### Testing + +Always authenticate before testing panel functionality. Filament uses Livewire, so use `Livewire::test()` or `livewire()` (available when `pestphp/pest-plugin-livewire` is in `composer.json`): + + +use function Pest\Livewire\livewire; + +livewire(ListUsers::class) + ->assertCanSeeTableRecords($users) + ->searchTable($users->first()->name) + ->assertCanSeeTableRecords($users->take(1)) + ->assertCanNotSeeTableRecords($users->skip(1)); + + + + +use function Pest\Laravel\assertDatabaseHas; +use function Pest\Livewire\livewire; + +livewire(CreateUser::class) + ->fillForm([ + 'name' => 'Test', + 'email' => 'test@example.com', + ]) + ->call('create') + ->assertNotified() + ->assertRedirect(); + +assertDatabaseHas(User::class, [ + 'name' => 'Test', + 'email' => 'test@example.com', +]); + + + + +use function Pest\Livewire\livewire; + +livewire(CreateUser::class) + ->fillForm([ + 'name' => null, + 'email' => 'invalid-email', + ]) + ->call('create') + ->assertHasFormErrors([ + 'name' => 'required', + 'email' => 'email', + ]) + ->assertNotNotified(); + + + + +use Filament\Actions\DeleteAction; +use function Pest\Livewire\livewire; + +livewire(EditUser::class, ['record' => $user->id]) + ->callAction(DeleteAction::class) + ->assertNotified() + ->assertRedirect(); + + + + +use Filament\Actions\Testing\TestAction; +use function Pest\Livewire\livewire; + +livewire(ListUsers::class) + ->callAction(TestAction::make('promote')->table($user), [ + 'role' => 'admin', + ]) + ->assertNotified(); + + + +### Correct Namespaces + +- Form fields (`TextInput`, `Select`, etc.): `Filament\Forms\Components\` +- Infolist entries (`TextEntry`, `IconEntry`, etc.): `Filament\Infolists\Components\` +- Layout components (`Grid`, `Section`, `Fieldset`, `Tabs`, `Wizard`, etc.): `Filament\Schemas\Components\` +- Schema utilities (`Get`, `Set`, etc.): `Filament\Schemas\Components\Utilities\` +- Actions (`DeleteAction`, `CreateAction`, etc.): `Filament\Actions\`. Never use `Filament\Tables\Actions\`, `Filament\Forms\Actions\`, or any other sub-namespace for actions. +- Icons: `Filament\Support\Icons\Heroicon` enum (e.g., `Heroicon::PencilSquare`) + +### Common Mistakes + +- **Never assume public file visibility.** File visibility is `private` by default. Always use `->visibility('public')` when public access is needed. +- **Never assume full-width layout.** `Grid`, `Section`, and `Fieldset` do not span all columns by default. Explicitly set column spans when needed. + +=== laravel/ai rules === + +## Laravel AI SDK + +- This application uses the Laravel AI SDK (`laravel/ai`) for all AI functionality. +- Activate the `developing-with-ai-sdk` skill when building, editing, updating, debugging, or testing AI agents, text generation, chat, streaming, structured output, tools, image generation, audio, transcription, embeddings, reranking, vector stores, files, conversation memory, or any AI provider integration (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter). + + diff --git a/Modules/Conversation/App/Events/ConversationReadUpdated.php b/Modules/Conversation/App/Events/ConversationReadUpdated.php new file mode 100644 index 000000000..b65931946 --- /dev/null +++ b/Modules/Conversation/App/Events/ConversationReadUpdated.php @@ -0,0 +1,37 @@ +userId.'.inbox'); + } + + public function broadcastAs(): string + { + return 'inbox.read.updated'; + } + + public function broadcastWith(): array + { + return $this->payload; + } +} diff --git a/Modules/Conversation/App/Events/InboxMessageCreated.php b/Modules/Conversation/App/Events/InboxMessageCreated.php new file mode 100644 index 000000000..87d2e9460 --- /dev/null +++ b/Modules/Conversation/App/Events/InboxMessageCreated.php @@ -0,0 +1,37 @@ +userId.'.inbox'); + } + + public function broadcastAs(): string + { + return 'inbox.message.created'; + } + + public function broadcastWith(): array + { + return $this->payload; + } +} diff --git a/Modules/Conversation/App/Http/Controllers/ConversationController.php b/Modules/Conversation/App/Http/Controllers/ConversationController.php index d11e548f4..6abbd033d 100644 --- a/Modules/Conversation/App/Http/Controllers/ConversationController.php +++ b/Modules/Conversation/App/Http/Controllers/ConversationController.php @@ -8,6 +8,8 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Schema; use Illuminate\View\View; +use Modules\Conversation\App\Events\ConversationReadUpdated; +use Modules\Conversation\App\Events\InboxMessageCreated; use Modules\Conversation\App\Models\Conversation; use Modules\Conversation\App\Models\ConversationMessage; use Modules\Conversation\App\Support\QuickMessageCatalog; @@ -28,20 +30,22 @@ class ConversationController extends Controller if ($userId && $this->messagingTablesReady()) { try { - $conversations = Conversation::inboxForUser($userId, $messageFilter); - $selectedConversation = Conversation::resolveSelected($conversations, $request->integer('conversation')); + [ + 'conversations' => $conversations, + 'selectedConversation' => $selectedConversation, + 'markedRead' => $markedRead, + ] = $this->resolveInboxState( + $userId, + $messageFilter, + $request->integer('conversation'), + true, + ); - if ($selectedConversation) { - $selectedConversation->loadThread(); - $selectedConversation->markAsReadFor($userId); - - $conversations = $conversations->map(function (Conversation $conversation) use ($selectedConversation): Conversation { - if ((int) $conversation->getKey() === (int) $selectedConversation->getKey()) { - $conversation->unread_count = 0; - } - - return $conversation; - }); + if ($selectedConversation && $markedRead) { + broadcast(new ConversationReadUpdated( + $userId, + $selectedConversation->readPayloadFor($userId), + )); } } catch (Throwable) { $conversations = collect(); @@ -58,6 +62,33 @@ class ConversationController extends Controller ]); } + public function state(Request $request): JsonResponse + { + abort_unless($this->messagingTablesReady(), 503, 'Messaging is not available yet.'); + + $userId = (int) $request->user()->getKey(); + $messageFilter = $this->resolveMessageFilter($request); + + [ + 'conversations' => $conversations, + 'selectedConversation' => $selectedConversation, + ] = $this->resolveInboxState( + $userId, + $messageFilter, + $request->integer('conversation'), + false, + ); + + return response()->json([ + 'list_html' => $this->renderInboxList($conversations, $messageFilter, $selectedConversation), + 'thread_html' => $this->renderInboxThread($selectedConversation, $messageFilter), + 'selected_conversation_id' => $selectedConversation ? (int) $selectedConversation->getKey() : null, + 'counts' => [ + 'unread_messages_total' => Conversation::unreadCountForUser($userId), + ], + ]); + } + public function start(Request $request, Listing $listing): RedirectResponse | JsonResponse { if (! $this->messagingTablesReady()) { @@ -98,12 +129,8 @@ class ConversationController extends Controller $message = null; if ($messageBody !== '') { - $message = $conversation->messages()->create([ - 'sender_id' => $user->getKey(), - 'body' => $messageBody, - ]); - - $conversation->forceFill(['last_message_at' => $message->created_at])->save(); + $message = $conversation->createMessageFor((int) $user->getKey(), $messageBody); + $this->broadcastMessageCreated($conversation, $message, (int) $user->getKey()); } if ($request->expectsJson()) { @@ -146,12 +173,8 @@ class ConversationController extends Controller return back()->with('error', 'Message cannot be empty.'); } - $message = $conversation->messages()->create([ - 'sender_id' => $userId, - 'body' => $messageBody, - ]); - - $conversation->forceFill(['last_message_at' => $message->created_at])->save(); + $message = $conversation->createMessageFor($userId, $messageBody); + $this->broadcastMessageCreated($conversation, $message, $userId); if ($request->expectsJson()) { return $this->conversationJsonResponse($conversation, $message, $userId); @@ -162,23 +185,43 @@ class ConversationController extends Controller ->with('success', 'Message sent.'); } + public function read(Request $request, Conversation $conversation): JsonResponse + { + abort_unless($this->messagingTablesReady(), 503, 'Messaging is not available yet.'); + + $userId = (int) $request->user()->getKey(); + abort_unless($conversation->hasParticipant($userId), 403); + + $updated = $conversation->markAsReadFor($userId); + $payload = $conversation->readPayloadFor($userId); + + if ($updated > 0) { + broadcast(new ConversationReadUpdated($userId, $payload))->toOthers(); + } + + return response()->json($payload); + } + private function conversationJsonResponse(Conversation $conversation, ?ConversationMessage $message, int $userId): JsonResponse { return response()->json([ 'conversation_id' => (int) $conversation->getKey(), 'send_url' => route('conversations.messages.send', $conversation), + 'read_url' => route('conversations.read', $conversation), + 'conversation' => [ + 'id' => (int) $conversation->getKey(), + 'unread_count' => $conversation->unreadCountForParticipant($userId), + ], + 'counts' => [ + 'unread_messages_total' => Conversation::unreadCountForUser($userId), + ], 'message' => $message ? $this->messagePayload($message, $userId) : null, ]); } private function messagePayload(ConversationMessage $message, int $userId): array { - return [ - 'id' => (int) $message->getKey(), - 'body' => (string) $message->body, - 'time' => $message->created_at?->format('H:i') ?? now()->format('H:i'), - 'is_mine' => (int) $message->sender_id === $userId, - ]; + return $message->toRealtimePayloadFor($userId); } private function inboxFilters(Request $request): array @@ -195,6 +238,78 @@ class ConversationController extends Controller return in_array($messageFilter, ['all', 'unread', 'important'], true) ? $messageFilter : 'all'; } + private function resolveInboxState( + int $userId, + string $messageFilter, + ?int $conversationId, + bool $markSelectedRead, + ): array { + $conversations = Conversation::inboxForUser($userId, $messageFilter); + $selectedConversation = Conversation::resolveSelected($conversations, $conversationId); + $markedRead = false; + + if ($selectedConversation) { + $selectedConversation->loadThread(); + + if ($markSelectedRead) { + $markedRead = $selectedConversation->markAsReadFor($userId) > 0; + $selectedConversation->unread_count = 0; + + $conversations = $conversations->map(function (Conversation $conversation) use ($selectedConversation): Conversation { + if ((int) $conversation->getKey() === (int) $selectedConversation->getKey()) { + $conversation->unread_count = 0; + } + + return $conversation; + }); + } + } + + return [ + 'conversations' => $conversations, + 'selectedConversation' => $selectedConversation, + 'markedRead' => $markedRead, + ]; + } + + private function renderInboxList($conversations, string $messageFilter, ?Conversation $selectedConversation): string + { + return view('conversation::partials.inbox-list-pane', [ + 'conversations' => $conversations, + 'messageFilter' => $messageFilter, + 'selectedConversation' => $selectedConversation, + ])->render(); + } + + private function renderInboxThread(?Conversation $selectedConversation, string $messageFilter): string + { + return view('conversation::partials.inbox-thread-pane', [ + 'selectedConversation' => $selectedConversation, + 'messageFilter' => $messageFilter, + 'quickMessages' => QuickMessageCatalog::all(), + ])->render(); + } + + private function broadcastMessageCreated( + Conversation $conversation, + ConversationMessage $message, + int $senderId, + ): void { + foreach ($conversation->participantIds() as $participantId) { + $event = new InboxMessageCreated( + $participantId, + $conversation->realtimePayloadFor($participantId, $message), + ); + + if ($participantId === $senderId) { + broadcast($event)->toOthers(); + continue; + } + + broadcast($event); + } + } + private function messagingTablesReady(): bool { try { diff --git a/Modules/Conversation/App/Models/Conversation.php b/Modules/Conversation/App/Models/Conversation.php index 8b355a8ca..2463d3689 100644 --- a/Modules/Conversation/App/Models/Conversation.php +++ b/Modules/Conversation/App/Models/Conversation.php @@ -121,13 +121,59 @@ class Conversation extends Model { $this->load([ 'listing:id,title,price,currency,user_id', - 'messages' => fn ($query) => $query->with('sender:id,name')->orderBy('created_at'), + 'messages' => fn ($query) => $query->with('sender:id,name')->ordered(), ]); } - public function markAsReadFor(int $userId): void + public function hasParticipant(int $userId): bool { - ConversationMessage::query() + return (int) $this->buyer_id === $userId || (int) $this->seller_id === $userId; + } + + public function participantIds(): array + { + return collect([$this->buyer_id, $this->seller_id]) + ->filter() + ->map(fn ($id): int => (int) $id) + ->unique() + ->values() + ->all(); + } + + public function partnerFor(int $userId): ?User + { + $this->loadMissing([ + 'buyer:id,name,email', + 'seller:id,name,email', + ]); + + return (int) $this->buyer_id === $userId ? $this->seller : $this->buyer; + } + + public function createMessageFor(int $senderId, string $body): ConversationMessage + { + $message = $this->messages()->create([ + 'sender_id' => $senderId, + 'body' => $body, + ]); + + $this->forceFill(['last_message_at' => $message->created_at])->save(); + + return $message->loadMissing('sender:id,name'); + } + + public function unreadCountForParticipant(int $userId): int + { + return (int) ConversationMessage::query() + ->where('conversation_id', $this->getKey()) + ->where('sender_id', '!=', $userId) + ->whereNull('read_at') + ->count(); + } + + public function markAsReadFor(int $userId): int + { + return ConversationMessage::query() ->where('conversation_id', $this->getKey()) ->where('sender_id', '!=', $userId) ->whereNull('read_at') @@ -137,6 +183,78 @@ class Conversation extends Model ]); } + public function listingImageUrl(): ?string + { + $this->loadMissing('listing'); + + $url = $this->listing?->getFirstMediaUrl('listing-images'); + + return is_string($url) && trim($url) !== '' ? $url : null; + } + + public function summaryPayloadFor(int $viewerId): array + { + $this->loadMissing([ + 'listing:id,title,price,currency,user_id', + 'buyer:id,name,email', + 'seller:id,name,email', + 'lastMessage', + 'lastMessage.sender:id,name', + ]); + + $lastMessage = $this->lastMessage; + $partner = $this->partnerFor($viewerId); + + return [ + 'id' => (int) $this->getKey(), + 'unread_count' => $this->unread_count ?? $this->unreadCountForParticipant($viewerId), + 'listing' => [ + 'id' => (int) ($this->listing?->getKey() ?? 0), + 'title' => (string) ($this->listing?->title ?? 'Listing removed'), + 'image_url' => $this->listingImageUrl(), + ], + 'partner' => [ + 'id' => (int) ($partner?->getKey() ?? 0), + 'name' => (string) ($partner?->name ?? 'User'), + ], + 'last_message' => $lastMessage + ? $lastMessage->toRealtimePayloadFor($viewerId) + : null, + 'last_message_at' => $this->last_message_at?->toIso8601String(), + ]; + } + + public function realtimePayloadFor(int $viewerId, ConversationMessage $message): array + { + $summary = $this->summaryPayloadFor($viewerId); + + return [ + 'conversation' => [ + 'id' => (int) $this->getKey(), + 'unread_count' => $this->unreadCountForParticipant($viewerId), + ], + 'listing' => $summary['listing'], + 'partner' => $summary['partner'], + 'message' => $message->toRealtimePayloadFor($viewerId), + 'counts' => [ + 'unread_messages_total' => static::unreadCountForUser($viewerId), + ], + ]; + } + + public function readPayloadFor(int $viewerId): array + { + return [ + 'conversation' => [ + 'id' => (int) $this->getKey(), + 'unread_count' => $this->unreadCountForParticipant($viewerId), + ], + 'counts' => [ + 'unread_messages_total' => static::unreadCountForUser($viewerId), + ], + ]; + } + public static function openForListingBuyer(Listing $listing, int $buyerId): self { $conversation = static::query()->firstOrCreate( diff --git a/Modules/Conversation/App/Models/ConversationMessage.php b/Modules/Conversation/App/Models/ConversationMessage.php index 520b0124c..405ce11c4 100644 --- a/Modules/Conversation/App/Models/ConversationMessage.php +++ b/Modules/Conversation/App/Models/ConversationMessage.php @@ -4,6 +4,7 @@ namespace Modules\Conversation\App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; use Modules\User\App\Models\User; class ConversationMessage extends Model @@ -23,4 +24,23 @@ class ConversationMessage extends Model { return $this->belongsTo(User::class, 'sender_id'); } + + public function scopeOrdered(Builder $query): Builder + { + return $query->orderBy('created_at'); + } + + public function toRealtimePayloadFor(int $viewerId): array + { + $this->loadMissing('sender:id,name'); + + return [ + 'id' => (int) $this->getKey(), + 'body' => (string) $this->body, + 'time' => $this->created_at?->format('H:i') ?? now()->format('H:i'), + 'sender_id' => (int) $this->sender_id, + 'sender_name' => (string) ($this->sender?->name ?? 'User'), + 'is_mine' => (int) $this->sender_id === $viewerId, + ]; + } } diff --git a/Modules/Conversation/App/Providers/ConversationServiceProvider.php b/Modules/Conversation/App/Providers/ConversationServiceProvider.php index 73dbdb972..50fb1ef9e 100644 --- a/Modules/Conversation/App/Providers/ConversationServiceProvider.php +++ b/Modules/Conversation/App/Providers/ConversationServiceProvider.php @@ -2,6 +2,7 @@ namespace Modules\Conversation\App\Providers; +use Illuminate\Support\Facades\Broadcast; use Illuminate\Support\ServiceProvider; class ConversationServiceProvider extends ServiceProvider @@ -11,6 +12,10 @@ class ConversationServiceProvider extends ServiceProvider $this->loadMigrationsFrom(module_path('Conversation', 'database/migrations')); $this->loadRoutesFrom(module_path('Conversation', 'routes/web.php')); $this->loadViewsFrom(module_path('Conversation', 'resources/views'), 'conversation'); + + Broadcast::channel('users.{id}.inbox', function ($user, $id): bool { + return (int) $user->getKey() === (int) $id; + }); } public function register(): void diff --git a/Modules/Conversation/resources/assets/js/conversation.js b/Modules/Conversation/resources/assets/js/conversation.js new file mode 100644 index 000000000..e54ad5a85 --- /dev/null +++ b/Modules/Conversation/resources/assets/js/conversation.js @@ -0,0 +1,578 @@ +const onReady = (callback) => { + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', callback, { once: true }); + return; + } + + callback(); +}; + +const csrfToken = () => document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || ''; + +const socketHeaders = () => { + const socketId = window.Echo?.socketId?.(); + + return socketId ? { 'X-Socket-ID': socketId } : {}; +}; + +const jsonHeaders = () => ({ + Accept: 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'X-CSRF-TOKEN': csrfToken(), + 'X-Requested-With': 'XMLHttpRequest', + ...socketHeaders(), +}); + +const toFormBody = (values) => { + const params = new URLSearchParams(); + + Object.entries(values).forEach(([key, value]) => { + if (value === null || value === undefined) { + return; + } + + params.append(key, String(value)); + }); + + return params.toString(); +}; + +const formatCount = (count) => (count > 99 ? '99+' : String(count)); + +const setBadgeCount = (badge, count) => { + if (!badge) { + return; + } + + if (!count || count < 1) { + badge.textContent = '0'; + badge.classList.add('hidden'); + return; + } + + badge.textContent = formatCount(count); + badge.classList.remove('hidden'); +}; + +const updateHeaderInboxBadge = (count) => { + setBadgeCount(document.querySelector('[data-header-inbox-badge]'), count); +}; + +const subscribeInboxChannel = () => { + const body = document.body; + const userId = String(body.dataset.authUserId || '').trim(); + const channelName = String(body.dataset.inboxChannel || '').trim(); + + if (!userId || !channelName || !window.Echo?.private || window.__ocInboxChannel === channelName) { + return; + } + + const channel = window.Echo.private(channelName); + + channel.listen('.inbox.message.created', (payload) => { + updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0); + document.dispatchEvent(new CustomEvent('oc:inbox-message-created', { detail: payload })); + }); + + channel.listen('.inbox.read.updated', (payload) => { + updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0); + document.dispatchEvent(new CustomEvent('oc:inbox-read-updated', { detail: payload })); + }); + + window.__ocInboxChannel = channelName; +}; + +const buildMessageItem = (message) => { + const item = document.createElement('div'); + item.dataset.messageId = String(message.id); + item.className = `lt-chat-item${message.is_mine ? ' is-mine' : ''}`; + + const bubble = document.createElement('div'); + bubble.className = 'lt-chat-bubble'; + bubble.textContent = message.body; + + const time = document.createElement('span'); + time.className = 'lt-chat-time'; + time.textContent = message.time || ''; + + item.append(bubble, time); + + return item; +}; + +const appendInlineChatMessage = (thread, emptyState, message) => { + if (!thread || !message?.body || thread.querySelector(`[data-message-id="${message.id}"]`)) { + return; + } + + const item = buildMessageItem(message); + thread.appendChild(item); + emptyState?.classList.add('is-hidden'); + thread.scrollTop = thread.scrollHeight; +}; + +const initInlineListingChat = () => { + const root = document.querySelector('[data-inline-chat]'); + + if (!root) { + return; + } + + const panel = root.querySelector('[data-inline-chat-panel]'); + const thread = root.querySelector('[data-inline-chat-thread]'); + const emptyState = root.querySelector('[data-inline-chat-empty]'); + const form = root.querySelector('[data-inline-chat-form]'); + const input = root.querySelector('[data-inline-chat-input]'); + const error = root.querySelector('[data-inline-chat-error]'); + const submitButton = root.querySelector('[data-inline-chat-submit]'); + const launcherBadge = root.querySelector('[data-inline-chat-badge]'); + + const currentConversationId = () => String(root.dataset.conversationId || '').trim(); + + const resolveReadUrl = () => { + if (root.dataset.readUrl) { + return root.dataset.readUrl; + } + + const conversationId = currentConversationId(); + const template = String(root.dataset.readUrlTemplate || ''); + + return conversationId && template ? template.replace('__CONVERSATION__', conversationId) : ''; + }; + + const setState = (state) => { + const isOpen = state === 'open' || state === 'sending'; + + root.dataset.state = state; + root.classList.toggle('is-open', isOpen); + root.classList.toggle('is-collapsed', state === 'collapsed'); + root.classList.toggle('is-sending', state === 'sending'); + + if (panel) { + panel.hidden = !isOpen; + } + + if (isOpen) { + window.requestAnimationFrame(() => { + thread?.scrollTo({ top: thread.scrollHeight }); + input?.focus(); + }); + } + }; + + const showError = (message) => { + if (!error) { + return; + } + + if (!message) { + error.textContent = ''; + error.classList.add('is-hidden'); + return; + } + + error.textContent = message; + error.classList.remove('is-hidden'); + }; + + const setUnreadCount = (count) => { + setBadgeCount(launcherBadge, count); + }; + + const markConversationRead = async () => { + const readUrl = resolveReadUrl(); + + if (!readUrl || !currentConversationId()) { + return; + } + + const response = await fetch(readUrl, { + method: 'POST', + headers: jsonHeaders(), + }); + + const payload = await response.json().catch(() => ({})); + + if (!response.ok) { + return; + } + + setUnreadCount(payload?.conversation?.unread_count ?? 0); + updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0); + }; + + document.querySelectorAll('[data-inline-chat-open]').forEach((button) => { + button.addEventListener('click', async () => { + showError(''); + setState('open'); + await markConversationRead(); + }); + }); + + root.querySelector('[data-inline-chat-close]')?.addEventListener('click', () => { + setState('collapsed'); + }); + + form?.addEventListener('submit', async (event) => { + event.preventDefault(); + + if (!input || !submitButton) { + return; + } + + const message = input.value.trim(); + if (message === '') { + showError('Message cannot be empty.'); + input.focus(); + return; + } + + const targetUrl = root.dataset.sendUrl || root.dataset.startUrl; + if (!targetUrl) { + showError('Messaging is not available right now.'); + return; + } + + showError(''); + submitButton.disabled = true; + setState('sending'); + + try { + const response = await fetch(targetUrl, { + method: 'POST', + headers: jsonHeaders(), + body: toFormBody({ message }), + }); + + const payload = await response.json().catch(() => ({})); + if (!response.ok) { + throw new Error(payload?.message || payload?.errors?.message?.[0] || 'Message could not be sent.'); + } + + if (payload.send_url) { + root.dataset.sendUrl = payload.send_url; + } + + if (payload.read_url) { + root.dataset.readUrl = payload.read_url; + } + + if (payload.conversation_id) { + root.dataset.conversationId = String(payload.conversation_id); + } + + if (payload.message) { + appendInlineChatMessage(thread, emptyState, payload.message); + } + + updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0); + setUnreadCount(payload?.conversation?.unread_count ?? 0); + input.value = ''; + setState('open'); + } catch (requestError) { + showError(requestError instanceof Error ? requestError.message : 'Message could not be sent.'); + setState('open'); + } finally { + submitButton.disabled = false; + } + }); + + document.addEventListener('oc:inbox-message-created', async ({ detail }) => { + if (String(detail?.conversation?.id || '') !== currentConversationId()) { + return; + } + + if (root.dataset.state === 'open') { + appendInlineChatMessage(thread, emptyState, detail.message); + + if (!detail?.message?.is_mine && document.visibilityState === 'visible') { + await markConversationRead(); + } + + return; + } + + if (!detail?.message?.is_mine) { + setUnreadCount(detail?.conversation?.unread_count ?? 0); + } + }); + + document.addEventListener('oc:inbox-read-updated', ({ detail }) => { + if (String(detail?.conversation?.id || '') !== currentConversationId()) { + return; + } + + setUnreadCount(detail?.conversation?.unread_count ?? 0); + }); + + document.addEventListener('visibilitychange', async () => { + if (document.visibilityState === 'visible' && root.dataset.state === 'open') { + await markConversationRead(); + } + }); + + setState('collapsed'); +}; + +const buildInboxMessageItem = (message) => { + const wrapper = document.createElement('div'); + wrapper.dataset.messageId = String(message.id); + wrapper.className = `mb-4 flex ${message.is_mine ? 'justify-end' : 'justify-start'}`; + + const shell = document.createElement('div'); + shell.className = 'max-w-[80%]'; + + const bubble = document.createElement('div'); + bubble.className = `${message.is_mine ? 'bg-amber-100 text-slate-900' : 'bg-white text-slate-900 border border-slate-200'} rounded-2xl px-4 py-2 text-base shadow-sm`; + bubble.textContent = message.body; + + const time = document.createElement('p'); + time.className = `text-xs text-slate-500 mt-1 ${message.is_mine ? 'text-right' : 'text-left'}`; + time.textContent = message.time || ''; + + shell.append(bubble, time); + wrapper.appendChild(shell); + + return wrapper; +}; + +const initInboxRealtime = () => { + const root = document.querySelector('[data-inbox-root]'); + + if (!root) { + return; + } + + const listContainer = root.querySelector('[data-inbox-list-container]'); + const threadContainer = root.querySelector('[data-inbox-thread-container]'); + + if (!listContainer || !threadContainer) { + return; + } + + const currentSelectedConversationId = () => { + const panel = threadContainer.querySelector('[data-inbox-thread-panel]'); + + return String( + root.dataset.selectedConversationId || + panel?.dataset.selectedConversationId || + '', + ).trim(); + }; + + const readUrlFor = (conversationId) => { + const template = String(root.dataset.readUrlTemplate || ''); + + return conversationId && template ? template.replace('__CONVERSATION__', conversationId) : ''; + }; + + const currentFilter = () => { + const params = new URLSearchParams(window.location.search); + + return params.get('message_filter') || 'all'; + }; + + const syncBrowserUrl = (conversationId) => { + const url = new URL(window.location.href); + const filter = currentFilter(); + + if (filter === 'all') { + url.searchParams.delete('message_filter'); + } else { + url.searchParams.set('message_filter', filter); + } + + if (conversationId) { + url.searchParams.set('conversation', conversationId); + } else { + url.searchParams.delete('conversation'); + } + + window.history.replaceState({}, '', url); + }; + + const refreshState = async ({ conversationId = currentSelectedConversationId(), scrollToBottom = false } = {}) => { + const params = new URLSearchParams(); + + if (currentFilter() !== 'all') { + params.set('message_filter', currentFilter()); + } + + if (conversationId) { + params.set('conversation', conversationId); + } + + const targetUrl = `${root.dataset.stateUrl}${params.toString() ? `?${params.toString()}` : ''}`; + const response = await fetch(targetUrl, { + headers: { + Accept: 'application/json', + 'X-Requested-With': 'XMLHttpRequest', + }, + }); + + if (!response.ok) { + return; + } + + const payload = await response.json().catch(() => null); + if (!payload) { + return; + } + + listContainer.innerHTML = payload.list_html || ''; + threadContainer.innerHTML = payload.thread_html || ''; + root.dataset.selectedConversationId = payload.selected_conversation_id ? String(payload.selected_conversation_id) : ''; + updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0); + syncBrowserUrl(root.dataset.selectedConversationId); + + if (scrollToBottom) { + const thread = threadContainer.querySelector('[data-inbox-thread]'); + thread?.scrollTo({ top: thread.scrollHeight }); + } + }; + + const showThreadError = (message) => { + const error = threadContainer.querySelector('[data-inbox-send-error]'); + + if (!error) { + return; + } + + if (!message) { + error.textContent = ''; + error.classList.add('hidden'); + return; + } + + error.textContent = message; + error.classList.remove('hidden'); + }; + + const appendInboxMessage = (message) => { + const thread = threadContainer.querySelector('[data-inbox-thread]'); + const emptyState = threadContainer.querySelector('[data-inbox-empty]'); + + if (!thread || !message?.body || thread.querySelector(`[data-message-id="${message.id}"]`)) { + return; + } + + if (emptyState) { + emptyState.remove(); + } + + thread.appendChild(buildInboxMessageItem(message)); + thread.scrollTop = thread.scrollHeight; + }; + + const markConversationRead = async (conversationId) => { + const readUrl = readUrlFor(conversationId); + + if (!readUrl) { + return; + } + + const response = await fetch(readUrl, { + method: 'POST', + headers: jsonHeaders(), + }); + + const payload = await response.json().catch(() => null); + + if (payload) { + updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0); + } + }; + + root.addEventListener('submit', async (event) => { + const form = event.target.closest('[data-inbox-send-form]'); + + if (!form) { + return; + } + + event.preventDefault(); + + const formData = new FormData(form); + const message = String(formData.get('message') || '').trim(); + const submitButton = form.querySelector('[data-inbox-send-button]') || form.querySelector('button[type="submit"]'); + const textInput = threadContainer.querySelector('[data-inbox-message-input]'); + + if (message === '') { + showThreadError('Message cannot be empty.'); + textInput?.focus(); + return; + } + + showThreadError(''); + submitButton?.setAttribute('disabled', 'disabled'); + + try { + const response = await fetch(form.action, { + method: 'POST', + headers: jsonHeaders(), + body: new URLSearchParams(formData).toString(), + }); + + const payload = await response.json().catch(() => ({})); + + if (!response.ok) { + throw new Error(payload?.message || payload?.errors?.message?.[0] || 'Message could not be sent.'); + } + + if (payload.message && String(payload.conversation_id || '') === currentSelectedConversationId()) { + appendInboxMessage(payload.message); + } + + if (textInput && form.contains(textInput)) { + textInput.value = ''; + textInput.focus(); + } + + root.dataset.selectedConversationId = String(payload.conversation_id || currentSelectedConversationId()); + await refreshState({ conversationId: root.dataset.selectedConversationId, scrollToBottom: true }); + } catch (requestError) { + showThreadError(requestError instanceof Error ? requestError.message : 'Message could not be sent.'); + } finally { + submitButton?.removeAttribute('disabled'); + } + }); + + document.addEventListener('oc:inbox-message-created', async ({ detail }) => { + const selectedConversationId = currentSelectedConversationId(); + const eventConversationId = String(detail?.conversation?.id || ''); + + if (selectedConversationId && eventConversationId === selectedConversationId) { + appendInboxMessage(detail.message); + + if (!detail?.message?.is_mine && document.visibilityState === 'visible') { + await markConversationRead(selectedConversationId); + } + + await refreshState({ conversationId: selectedConversationId, scrollToBottom: true }); + return; + } + + await refreshState({ conversationId: selectedConversationId }); + }); + + document.addEventListener('oc:inbox-read-updated', async () => { + await refreshState({ conversationId: currentSelectedConversationId() }); + }); + + document.addEventListener('visibilitychange', async () => { + const selectedConversationId = currentSelectedConversationId(); + + if (document.visibilityState !== 'visible' || !selectedConversationId) { + return; + } + + await markConversationRead(selectedConversationId); + await refreshState({ conversationId: selectedConversationId }); + }); +}; + +onReady(() => { + subscribeInboxChannel(); + initInlineListingChat(); + initInboxRealtime(); +}); diff --git a/Modules/Conversation/resources/views/inbox.blade.php b/Modules/Conversation/resources/views/inbox.blade.php index d40c0d5ad..053ec63fb 100644 --- a/Modules/Conversation/resources/views/inbox.blade.php +++ b/Modules/Conversation/resources/views/inbox.blade.php @@ -16,7 +16,15 @@ : null, ]) -
+
@if($requiresLogin ?? false)
@@ -27,146 +35,20 @@ @endif
-
- -
- @forelse($conversations as $conversation) - @php - $conversationListing = $conversation->listing; - $partner = (int) $conversation->buyer_id === (int) auth()->id() ? $conversation->seller : $conversation->buyer; - $isSelected = $selectedConversation && (int) $selectedConversation->id === (int) $conversation->id; - $conversationImage = $conversationListing?->getFirstMediaUrl('listing-images'); - $lastMessage = trim((string) ($conversation->lastMessage?->body ?? '')); - @endphp - -
-
- @if($conversationImage) - {{ $conversationListing?->title }} - @else -
Listing
- @endif -
-
-
-

{{ $partner?->name ?? 'User' }}

-

{{ $conversation->last_message_at?->format('d.m.Y') }}

-
-

{{ $conversationListing?->title ?? 'Listing removed' }}

-

- {{ $lastMessage !== '' ? $lastMessage : 'No messages yet' }} -

-
- @if($conversation->unread_count > 0) - - {{ $conversation->unread_count }} - - @endif -
-
- @empty -
- No conversations yet. -
- @endforelse -
+
+ @include('conversation::partials.inbox-list-pane', [ + 'conversations' => $conversations, + 'messageFilter' => $messageFilter, + 'selectedConversation' => $selectedConversation, + ])
-
- @if($selectedConversation) - @php - $activeListing = $selectedConversation->listing; - $activePartner = (int) $selectedConversation->buyer_id === (int) auth()->id() - ? $selectedConversation->seller - : $selectedConversation->buyer; - $activePriceLabel = $activeListing && !is_null($activeListing->price) - ? number_format((float) $activeListing->price, 0).' '.($activeListing->currency ?? 'TL') - : null; - @endphp -
-
- {{ strtoupper(substr((string) ($activePartner?->name ?? 'K'), 0, 1)) }} -
-
-

{{ $activePartner?->name ?? 'User' }}

-

{{ $activeListing?->title ?? 'Listing removed' }}

-
- @if($activePriceLabel) -
{{ $activePriceLabel }}
- @endif -
- -
- @forelse($selectedConversation->messages as $message) - @php $isMine = (int) $message->sender_id === (int) auth()->id(); @endphp -
-
-
- {{ $message->body }} -
-

- {{ $message->created_at?->format('H:i') }} -

-
-
- @empty -
-
-

No messages yet.

-

Use a quick reply or send the first message below.

-
-
- @endforelse -
- -
-
- @foreach($quickMessages as $quickMessage) -
- @csrf - - - -
- @endforeach -
-
- @csrf - - - -
- @error('message') -

{{ $message }}

- @enderror -
- @else -
-
-

Choose a conversation to start messaging.

-

Start a new chat from a listing detail page or continue an existing thread here.

-
-
- @endif +
+ @include('conversation::partials.inbox-thread-pane', [ + 'selectedConversation' => $selectedConversation, + 'messageFilter' => $messageFilter, + 'quickMessages' => $quickMessages, + ])
diff --git a/Modules/Conversation/resources/views/partials/inbox-list-pane.blade.php b/Modules/Conversation/resources/views/partials/inbox-list-pane.blade.php new file mode 100644 index 000000000..82e8909ff --- /dev/null +++ b/Modules/Conversation/resources/views/partials/inbox-list-pane.blade.php @@ -0,0 +1,57 @@ +
+ +
+ @forelse($conversations as $conversation) + @php + $conversationListing = $conversation->listing; + $partner = (int) $conversation->buyer_id === (int) auth()->id() ? $conversation->seller : $conversation->buyer; + $isSelected = $selectedConversation && (int) $selectedConversation->id === (int) $conversation->id; + $conversationImage = $conversationListing?->getFirstMediaUrl('listing-images'); + $lastMessage = trim((string) ($conversation->lastMessage?->body ?? '')); + @endphp + +
+
+ @if($conversationImage) + {{ $conversationListing?->title }} + @else +
Listing
+ @endif +
+
+
+

{{ $partner?->name ?? 'User' }}

+

{{ $conversation->last_message_at?->format('d.m.Y') }}

+
+

{{ $conversationListing?->title ?? 'Listing removed' }}

+

+ {{ $lastMessage !== '' ? $lastMessage : 'No messages yet' }} +

+
+ @if($conversation->unread_count > 0) + + {{ $conversation->unread_count }} + + @endif +
+
+ @empty +
+ No conversations yet. +
+ @endforelse +
+
diff --git a/Modules/Conversation/resources/views/partials/inbox-thread-pane.blade.php b/Modules/Conversation/resources/views/partials/inbox-thread-pane.blade.php new file mode 100644 index 000000000..9e54a4fcc --- /dev/null +++ b/Modules/Conversation/resources/views/partials/inbox-thread-pane.blade.php @@ -0,0 +1,81 @@ +
+ @if($selectedConversation) + @php + $activeListing = $selectedConversation->listing; + $activePartner = (int) $selectedConversation->buyer_id === (int) auth()->id() + ? $selectedConversation->seller + : $selectedConversation->buyer; + $activePriceLabel = $activeListing && !is_null($activeListing->price) + ? number_format((float) $activeListing->price, 0).' '.($activeListing->currency ?? 'TL') + : null; + @endphp +
+
+ {{ strtoupper(substr((string) ($activePartner?->name ?? 'K'), 0, 1)) }} +
+
+

{{ $activePartner?->name ?? 'User' }}

+

{{ $activeListing?->title ?? 'Listing removed' }}

+
+ @if($activePriceLabel) +
{{ $activePriceLabel }}
+ @endif +
+ +
+ @forelse($selectedConversation->messages as $message) + @php $isMine = (int) $message->sender_id === (int) auth()->id(); @endphp +
+
+
+ {{ $message->body }} +
+

+ {{ $message->created_at?->format('H:i') }} +

+
+
+ @empty +
+
+

No messages yet.

+

Use a quick reply or send the first message below.

+
+
+ @endforelse +
+ +
+
+ @foreach($quickMessages as $quickMessage) +
+ @csrf + + + +
+ @endforeach +
+
+ @csrf + + + +
+ +
+ @else +
+
+

Choose a conversation to start messaging.

+

Start a new chat from a listing detail page or continue an existing thread here.

+
+
+ @endif +
diff --git a/Modules/Conversation/routes/web.php b/Modules/Conversation/routes/web.php index b5acdfb42..c4832c66b 100644 --- a/Modules/Conversation/routes/web.php +++ b/Modules/Conversation/routes/web.php @@ -6,10 +6,12 @@ use Modules\Conversation\App\Http\Controllers\ConversationController; Route::middleware('web')->group(function () { Route::prefix('panel')->name('panel.')->group(function () { Route::get('/inbox', [ConversationController::class, 'inbox'])->name('inbox.index'); + Route::middleware('auth')->get('/inbox/state', [ConversationController::class, 'state'])->name('inbox.state'); }); Route::middleware('auth')->name('conversations.')->group(function () { Route::post('/listings/{listing}/conversation', [ConversationController::class, 'start'])->name('start'); Route::post('/conversations/{conversation}/messages', [ConversationController::class, 'send'])->name('messages.send'); + Route::post('/conversations/{conversation}/read', [ConversationController::class, 'read'])->name('read'); }); }); diff --git a/Modules/Listing/Http/Controllers/ListingController.php b/Modules/Listing/Http/Controllers/ListingController.php index 6efcc8945..452b250ab 100644 --- a/Modules/Listing/Http/Controllers/ListingController.php +++ b/Modules/Listing/Http/Controllers/ListingController.php @@ -188,7 +188,6 @@ class ListingController extends Controller $isListingFavorited = false; $isSellerFavorited = false; - $existingConversationId = null; $detailConversation = null; if (auth()->check()) { @@ -219,7 +218,11 @@ class ListingController extends Controller if ($detailConversation) { $detailConversation->loadThread(); - $detailConversation->markAsReadFor($userId); + $detailConversation->loadCount([ + 'messages as unread_count' => fn ($query) => $query + ->where('sender_id', '!=', $userId) + ->whereNull('read_at'), + ]); } } } @@ -230,7 +233,6 @@ class ListingController extends Controller 'isListingFavorited', 'isSellerFavorited', 'presentableCustomFields', - 'existingConversationId', 'detailConversation', 'gallery', 'listingVideos', diff --git a/Modules/Listing/resources/views/themes/otoplus/show.blade.php b/Modules/Listing/resources/views/themes/otoplus/show.blade.php index 01c63e4fe..446c121a9 100644 --- a/Modules/Listing/resources/views/themes/otoplus/show.blade.php +++ b/Modules/Listing/resources/views/themes/otoplus/show.blade.php @@ -45,7 +45,9 @@ $chatConversation = $detailConversation ?? null; $chatMessages = $chatConversation?->messages ?? collect(); $chatSendUrl = $chatConversation ? route('conversations.messages.send', $chatConversation) : ''; + $chatReadUrl = $chatConversation ? route('conversations.read', $chatConversation) : ''; $chatStartUrl = route('conversations.start', $listing); + $chatUnreadCount = max(0, (int) ($chatConversation?->unread_count ?? 0)); $primaryContactHref = null; $primaryContactLabel = 'Call'; @@ -400,7 +402,23 @@
@if($canStartConversation) -
+