2026-04-16 00:23:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
|
|
feat: complete AI assistant with custom services and brand config
Baseline snapshot of custom AI implementation before Laravel AI SDK migration.
Includes:
- Custom services: GeminiTextGenerationService, TextGenerationService (OpenAI), ImageGenerationService, AudioGenerationService, VideoGenerationService
- IntentDetector for content moderation via keyword matching
- AI enums: Intent, Orientation, UsageType
- Blade prompt templates: system.blade.php, image.blade.php, video.blade.php
- AiMessage with content_html accessor (markdown rendering)
- AiUsageLog for monthly quota tracking per account
- PostAssistantController with regex-based [GENERATE_*] parsing
- WritingAssistantTab with markdown rendering, add-to-post, attachments
- Workspace brand fields (name, description, tone, voice_notes) in system prompt
- Session state block injected into prompts (thread counts, quota remaining)
- AttachmentCollector pattern will replace the regex approach in Phase 2
- Post comments with replies, emoji reactions, real-time via Echo
- Assets page with Unsplash + Giphy integrations
2026-04-16 12:25:08 +00:00
|
|
|
use App\Enums\Ai\Intent;
|
2026-04-17 02:05:51 +00:00
|
|
|
use App\Enums\AiMessage\Status;
|
feat: complete AI assistant with custom services and brand config
Baseline snapshot of custom AI implementation before Laravel AI SDK migration.
Includes:
- Custom services: GeminiTextGenerationService, TextGenerationService (OpenAI), ImageGenerationService, AudioGenerationService, VideoGenerationService
- IntentDetector for content moderation via keyword matching
- AI enums: Intent, Orientation, UsageType
- Blade prompt templates: system.blade.php, image.blade.php, video.blade.php
- AiMessage with content_html accessor (markdown rendering)
- AiUsageLog for monthly quota tracking per account
- PostAssistantController with regex-based [GENERATE_*] parsing
- WritingAssistantTab with markdown rendering, add-to-post, attachments
- Workspace brand fields (name, description, tone, voice_notes) in system prompt
- Session state block injected into prompts (thread counts, quota remaining)
- AttachmentCollector pattern will replace the regex approach in Phase 2
- Post comments with replies, emoji reactions, real-time via Echo
- Assets page with Unsplash + Giphy integrations
2026-04-16 12:25:08 +00:00
|
|
|
use App\Http\Requests\App\Assistant\StoreAssistantMessageRequest;
|
2026-04-17 02:05:51 +00:00
|
|
|
use App\Jobs\Ai\GenerateAssistantResponse;
|
2026-04-16 00:23:13 +00:00
|
|
|
use App\Models\Post;
|
|
|
|
|
use App\Services\Ai\IntentDetector;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
class PostAssistantController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index(Request $request, Post $post): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if ($post->workspace_id !== $workspace->id) {
|
|
|
|
|
abort(Response::HTTP_FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$messages = $post->aiMessages()
|
|
|
|
|
->with('user')
|
|
|
|
|
->oldest()
|
2026-04-17 02:05:51 +00:00
|
|
|
->orderBy('id')
|
2026-04-16 00:23:13 +00:00
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
return response()->json(['messages' => $messages]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(
|
feat: complete AI assistant with custom services and brand config
Baseline snapshot of custom AI implementation before Laravel AI SDK migration.
Includes:
- Custom services: GeminiTextGenerationService, TextGenerationService (OpenAI), ImageGenerationService, AudioGenerationService, VideoGenerationService
- IntentDetector for content moderation via keyword matching
- AI enums: Intent, Orientation, UsageType
- Blade prompt templates: system.blade.php, image.blade.php, video.blade.php
- AiMessage with content_html accessor (markdown rendering)
- AiUsageLog for monthly quota tracking per account
- PostAssistantController with regex-based [GENERATE_*] parsing
- WritingAssistantTab with markdown rendering, add-to-post, attachments
- Workspace brand fields (name, description, tone, voice_notes) in system prompt
- Session state block injected into prompts (thread counts, quota remaining)
- AttachmentCollector pattern will replace the regex approach in Phase 2
- Post comments with replies, emoji reactions, real-time via Echo
- Assets page with Unsplash + Giphy integrations
2026-04-16 12:25:08 +00:00
|
|
|
StoreAssistantMessageRequest $request,
|
2026-04-16 00:23:13 +00:00
|
|
|
Post $post,
|
|
|
|
|
IntentDetector $intentDetector,
|
|
|
|
|
): JsonResponse {
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if ($post->workspace_id !== $workspace->id) {
|
|
|
|
|
abort(Response::HTTP_FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
|
feat: complete AI assistant with custom services and brand config
Baseline snapshot of custom AI implementation before Laravel AI SDK migration.
Includes:
- Custom services: GeminiTextGenerationService, TextGenerationService (OpenAI), ImageGenerationService, AudioGenerationService, VideoGenerationService
- IntentDetector for content moderation via keyword matching
- AI enums: Intent, Orientation, UsageType
- Blade prompt templates: system.blade.php, image.blade.php, video.blade.php
- AiMessage with content_html accessor (markdown rendering)
- AiUsageLog for monthly quota tracking per account
- PostAssistantController with regex-based [GENERATE_*] parsing
- WritingAssistantTab with markdown rendering, add-to-post, attachments
- Workspace brand fields (name, description, tone, voice_notes) in system prompt
- Session state block injected into prompts (thread counts, quota remaining)
- AttachmentCollector pattern will replace the regex approach in Phase 2
- Post comments with replies, emoji reactions, real-time via Echo
- Assets page with Unsplash + Giphy integrations
2026-04-16 12:25:08 +00:00
|
|
|
$validated = $request->validated();
|
2026-04-16 00:23:13 +00:00
|
|
|
|
|
|
|
|
$prompt = data_get($validated, 'body');
|
|
|
|
|
|
|
|
|
|
$userMessage = $post->aiMessages()->create([
|
|
|
|
|
'user_id' => $request->user()->id,
|
|
|
|
|
'role' => 'user',
|
|
|
|
|
'content' => $prompt,
|
2026-04-17 02:05:51 +00:00
|
|
|
'status' => Status::Completed,
|
2026-04-16 00:23:13 +00:00
|
|
|
]);
|
|
|
|
|
|
feat: complete AI assistant with custom services and brand config
Baseline snapshot of custom AI implementation before Laravel AI SDK migration.
Includes:
- Custom services: GeminiTextGenerationService, TextGenerationService (OpenAI), ImageGenerationService, AudioGenerationService, VideoGenerationService
- IntentDetector for content moderation via keyword matching
- AI enums: Intent, Orientation, UsageType
- Blade prompt templates: system.blade.php, image.blade.php, video.blade.php
- AiMessage with content_html accessor (markdown rendering)
- AiUsageLog for monthly quota tracking per account
- PostAssistantController with regex-based [GENERATE_*] parsing
- WritingAssistantTab with markdown rendering, add-to-post, attachments
- Workspace brand fields (name, description, tone, voice_notes) in system prompt
- Session state block injected into prompts (thread counts, quota remaining)
- AttachmentCollector pattern will replace the regex approach in Phase 2
- Post comments with replies, emoji reactions, real-time via Echo
- Assets page with Unsplash + Giphy integrations
2026-04-16 12:25:08 +00:00
|
|
|
if ($request->hasFile('image')) {
|
|
|
|
|
$media = $workspace->addMedia($request->file('image'), 'assets');
|
|
|
|
|
|
|
|
|
|
$userMessage->update([
|
|
|
|
|
'attachments' => [['id' => $media->id, 'path' => $media->path, 'url' => $media->url, 'type' => 'image', 'mime_type' => $media->mime_type]],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 00:23:13 +00:00
|
|
|
$userMessage->load('user');
|
|
|
|
|
|
|
|
|
|
$intent = $intentDetector->detect($prompt);
|
|
|
|
|
|
feat: complete AI assistant with custom services and brand config
Baseline snapshot of custom AI implementation before Laravel AI SDK migration.
Includes:
- Custom services: GeminiTextGenerationService, TextGenerationService (OpenAI), ImageGenerationService, AudioGenerationService, VideoGenerationService
- IntentDetector for content moderation via keyword matching
- AI enums: Intent, Orientation, UsageType
- Blade prompt templates: system.blade.php, image.blade.php, video.blade.php
- AiMessage with content_html accessor (markdown rendering)
- AiUsageLog for monthly quota tracking per account
- PostAssistantController with regex-based [GENERATE_*] parsing
- WritingAssistantTab with markdown rendering, add-to-post, attachments
- Workspace brand fields (name, description, tone, voice_notes) in system prompt
- Session state block injected into prompts (thread counts, quota remaining)
- AttachmentCollector pattern will replace the regex approach in Phase 2
- Post comments with replies, emoji reactions, real-time via Echo
- Assets page with Unsplash + Giphy integrations
2026-04-16 12:25:08 +00:00
|
|
|
if ($intent === Intent::Blocked) {
|
|
|
|
|
$assistantMessage = $post->aiMessages()->create([
|
|
|
|
|
'role' => 'assistant',
|
|
|
|
|
'content' => __('assistant.content_blocked'),
|
2026-04-17 02:05:51 +00:00
|
|
|
'status' => Status::Completed,
|
feat: complete AI assistant with custom services and brand config
Baseline snapshot of custom AI implementation before Laravel AI SDK migration.
Includes:
- Custom services: GeminiTextGenerationService, TextGenerationService (OpenAI), ImageGenerationService, AudioGenerationService, VideoGenerationService
- IntentDetector for content moderation via keyword matching
- AI enums: Intent, Orientation, UsageType
- Blade prompt templates: system.blade.php, image.blade.php, video.blade.php
- AiMessage with content_html accessor (markdown rendering)
- AiUsageLog for monthly quota tracking per account
- PostAssistantController with regex-based [GENERATE_*] parsing
- WritingAssistantTab with markdown rendering, add-to-post, attachments
- Workspace brand fields (name, description, tone, voice_notes) in system prompt
- Session state block injected into prompts (thread counts, quota remaining)
- AttachmentCollector pattern will replace the regex approach in Phase 2
- Post comments with replies, emoji reactions, real-time via Echo
- Assets page with Unsplash + Giphy integrations
2026-04-16 12:25:08 +00:00
|
|
|
'metadata' => ['intent' => $intent->value, 'error' => true],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'user_message' => $userMessage,
|
|
|
|
|
'assistant_message' => $assistantMessage,
|
|
|
|
|
], Response::HTTP_CREATED);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 02:05:51 +00:00
|
|
|
$assistantMessage = $post->aiMessages()->create([
|
|
|
|
|
'role' => 'assistant',
|
|
|
|
|
'content' => '',
|
|
|
|
|
'status' => Status::Pending,
|
|
|
|
|
'metadata' => ['intent' => $intent->value],
|
|
|
|
|
]);
|
2026-04-16 00:23:13 +00:00
|
|
|
|
2026-04-17 02:05:51 +00:00
|
|
|
GenerateAssistantResponse::dispatch(
|
|
|
|
|
assistantMessage: $assistantMessage,
|
|
|
|
|
prompt: $prompt,
|
|
|
|
|
intent: $intent->value,
|
|
|
|
|
);
|
2026-04-16 00:23:13 +00:00
|
|
|
|
2026-04-17 02:05:51 +00:00
|
|
|
return response()->json([
|
|
|
|
|
'user_message' => $userMessage,
|
|
|
|
|
'assistant_message' => $assistantMessage,
|
|
|
|
|
], Response::HTTP_ACCEPTED);
|
2026-04-16 00:23:13 +00:00
|
|
|
}
|
|
|
|
|
}
|