AI Assistant — multi-provider chat (Claude / GPT / Gemini)
Schema: - ai_chats: company_id, user_id, title, provider; index pe activitate - ai_messages: role (system/user/assistant), content, meta JSON (tokens, latency, model) Service AiAssistantService (multi-provider): - ask($chat, $message): persistă mesajul user, build system prompt cu context tenant (statistici clienți/mașini/cereri/datorii), apelează API-ul providerului, persistă răspunsul cu meta (tokens, latency) - callClaude: api.anthropic.com/v1/messages cu claude-sonnet-4-5 - callOpenAI: api.openai.com/v1/chat/completions cu gpt-4o-mini - callGemini: generativelanguage.googleapis.com cu gemini-1.5-flash - Try/catch pe toate; eroare devine mesaj asistent fără să crape System prompt include: - Numele și orașul companiei - Statistici curente (clienți, mașini, cereri noi, fișe active, datorii) - Limita stricta: NU inventează date Custom Filament Page /app/ai-assistant (group Analiză): - Sidebar stâng: listă conversații (last 20), buton 'Nouă' + delete cu confirm - Main: bubble chat (user dreapta albastru, asistent stânga gri) - Meta jos pe răspuns: provider · latency · tokens - Empty state friendly cu instrucțiuni configurare - Loading indicator (3 dots animate) când AI răspunde - Auto-scroll la mesaj nou - Enter trimite, Shift+Enter newline - Auto-titlu chat din primul mesaj user (60 chars) Settings page extins cu secțiune 'Asistent AI': - Provider implicit (claude/gpt/gemini) - 3 chei API (password fields, revealable) - Key-urile salvate în companies.settings.ai (per tenant, izolat)
This commit is contained in:
@@ -0,0 +1,112 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Tenant\Pages;
|
||||||
|
|
||||||
|
use App\Models\Tenant\AiChat;
|
||||||
|
use App\Services\Ai\AiAssistantService;
|
||||||
|
use App\Tenancy\TenantManager;
|
||||||
|
use Filament\Pages\Page;
|
||||||
|
use Livewire\Attributes\Url;
|
||||||
|
|
||||||
|
class AiAssistant extends Page
|
||||||
|
{
|
||||||
|
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-sparkles';
|
||||||
|
|
||||||
|
protected static ?string $navigationLabel = 'Asistent AI';
|
||||||
|
|
||||||
|
protected static string|\UnitEnum|null $navigationGroup = 'Analiză';
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 71;
|
||||||
|
|
||||||
|
protected static ?string $title = 'Asistent AI';
|
||||||
|
|
||||||
|
protected string $view = 'filament.tenant.pages.ai-assistant';
|
||||||
|
|
||||||
|
public ?int $chatId = null;
|
||||||
|
|
||||||
|
public string $newMessage = '';
|
||||||
|
|
||||||
|
public bool $loading = false;
|
||||||
|
|
||||||
|
public function mount(): void
|
||||||
|
{
|
||||||
|
// Use last chat for the user, or create a new one.
|
||||||
|
$userId = auth()->id();
|
||||||
|
$companyId = app(TenantManager::class)->currentId();
|
||||||
|
if (! $userId || ! $companyId) return;
|
||||||
|
|
||||||
|
$chat = AiChat::where('user_id', $userId)->latest('updated_at')->first();
|
||||||
|
if (! $chat) {
|
||||||
|
$chat = AiChat::create([
|
||||||
|
'company_id' => $companyId,
|
||||||
|
'user_id' => $userId,
|
||||||
|
'title' => 'Conversație nouă',
|
||||||
|
'provider' => $this->defaultProvider(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
$this->chatId = $chat->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChat(): ?AiChat
|
||||||
|
{
|
||||||
|
return $this->chatId ? AiChat::with('messages')->find($this->chatId) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChats()
|
||||||
|
{
|
||||||
|
return AiChat::where('user_id', auth()->id())
|
||||||
|
->latest('updated_at')
|
||||||
|
->limit(20)
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function newChat(): void
|
||||||
|
{
|
||||||
|
$chat = AiChat::create([
|
||||||
|
'company_id' => app(TenantManager::class)->currentId(),
|
||||||
|
'user_id' => auth()->id(),
|
||||||
|
'title' => 'Conversație nouă',
|
||||||
|
'provider' => $this->defaultProvider(),
|
||||||
|
]);
|
||||||
|
$this->chatId = $chat->id;
|
||||||
|
$this->newMessage = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function selectChat(int $id): void
|
||||||
|
{
|
||||||
|
$chat = AiChat::where('user_id', auth()->id())->where('id', $id)->first();
|
||||||
|
if ($chat) $this->chatId = $chat->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteChat(int $id): void
|
||||||
|
{
|
||||||
|
AiChat::where('user_id', auth()->id())->where('id', $id)->delete();
|
||||||
|
if ($this->chatId === $id) {
|
||||||
|
$this->chatId = AiChat::where('user_id', auth()->id())->latest('updated_at')->value('id');
|
||||||
|
if (! $this->chatId) $this->newChat();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send(): void
|
||||||
|
{
|
||||||
|
$msg = trim($this->newMessage);
|
||||||
|
if ($msg === '' || ! $this->chatId) return;
|
||||||
|
|
||||||
|
$this->loading = true;
|
||||||
|
$this->newMessage = '';
|
||||||
|
$chat = AiChat::find($this->chatId);
|
||||||
|
if (! $chat) { $this->loading = false; return; }
|
||||||
|
|
||||||
|
try {
|
||||||
|
app(AiAssistantService::class)->ask($chat, $msg);
|
||||||
|
} finally {
|
||||||
|
$this->loading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defaultProvider(): string
|
||||||
|
{
|
||||||
|
$tenant = app(TenantManager::class)->current();
|
||||||
|
return ($tenant?->settings['ai']['default_provider'] ?? 'claude');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,6 +50,10 @@ class Settings extends Page
|
|||||||
'notify_payment' => $notify['payment'] ?? true,
|
'notify_payment' => $notify['payment'] ?? true,
|
||||||
'notify_appointment' => $notify['appointment'] ?? true,
|
'notify_appointment' => $notify['appointment'] ?? true,
|
||||||
'notify_reminder' => $notify['reminder'] ?? true,
|
'notify_reminder' => $notify['reminder'] ?? true,
|
||||||
|
'ai_default_provider' => $settings['ai']['default_provider'] ?? 'claude',
|
||||||
|
'ai_claude_key' => $settings['ai']['claude_key'] ?? null,
|
||||||
|
'ai_gpt_key' => $settings['ai']['gpt_key'] ?? null,
|
||||||
|
'ai_gemini_key' => $settings['ai']['gemini_key'] ?? null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,6 +124,18 @@ class Settings extends Page
|
|||||||
Forms\Components\Toggle::make('notify_appointment')->label('Programare confirmată')->default(true),
|
Forms\Components\Toggle::make('notify_appointment')->label('Programare confirmată')->default(true),
|
||||||
Forms\Components\Toggle::make('notify_reminder')->label('Reminder ITP / revizie')->default(true),
|
Forms\Components\Toggle::make('notify_reminder')->label('Reminder ITP / revizie')->default(true),
|
||||||
]),
|
]),
|
||||||
|
Schemas\Components\Section::make('Asistent AI')
|
||||||
|
->description('Adaugă chei API ca să activezi asistentul. Cheile rămân la voi — nu sunt partajate.')
|
||||||
|
->columns(2)
|
||||||
|
->schema([
|
||||||
|
Forms\Components\Select::make('ai_default_provider')
|
||||||
|
->label('Provider implicit')
|
||||||
|
->options(['claude' => 'Claude (Anthropic)', 'gpt' => 'ChatGPT (OpenAI)', 'gemini' => 'Gemini (Google)'])
|
||||||
|
->default('claude'),
|
||||||
|
Forms\Components\TextInput::make('ai_claude_key')->label('Claude API Key')->password()->revealable()->placeholder('sk-ant-...'),
|
||||||
|
Forms\Components\TextInput::make('ai_gpt_key')->label('OpenAI API Key')->password()->revealable()->placeholder('sk-proj-...'),
|
||||||
|
Forms\Components\TextInput::make('ai_gemini_key')->label('Gemini API Key')->password()->revealable(),
|
||||||
|
]),
|
||||||
])
|
])
|
||||||
->statePath('data');
|
->statePath('data');
|
||||||
}
|
}
|
||||||
@@ -151,6 +167,12 @@ class Settings extends Page
|
|||||||
'appointment' => (bool) ($data['notify_appointment'] ?? true),
|
'appointment' => (bool) ($data['notify_appointment'] ?? true),
|
||||||
'reminder' => (bool) ($data['notify_reminder'] ?? true),
|
'reminder' => (bool) ($data['notify_reminder'] ?? true),
|
||||||
],
|
],
|
||||||
|
'ai' => [
|
||||||
|
'default_provider' => $data['ai_default_provider'] ?? 'claude',
|
||||||
|
'claude_key' => $data['ai_claude_key'] ?? null,
|
||||||
|
'gpt_key' => $data['ai_gpt_key'] ?? null,
|
||||||
|
'gemini_key' => $data['ai_gemini_key'] ?? null,
|
||||||
|
],
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Tenant;
|
||||||
|
|
||||||
|
use App\Models\Concerns\BelongsToTenant;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
|
class AiChat extends Model
|
||||||
|
{
|
||||||
|
use BelongsToTenant;
|
||||||
|
|
||||||
|
protected $fillable = ['company_id', 'user_id', 'title', 'provider'];
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function messages(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(AiMessage::class)->orderBy('created_at');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Tenant;
|
||||||
|
|
||||||
|
use App\Models\Concerns\BelongsToTenant;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
class AiMessage extends Model
|
||||||
|
{
|
||||||
|
use BelongsToTenant;
|
||||||
|
|
||||||
|
protected $fillable = ['company_id', 'ai_chat_id', 'role', 'content', 'meta'];
|
||||||
|
|
||||||
|
protected $casts = ['meta' => 'array'];
|
||||||
|
|
||||||
|
public function chat(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(AiChat::class, 'ai_chat_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,206 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Ai;
|
||||||
|
|
||||||
|
use App\Models\Central\Company;
|
||||||
|
use App\Models\Tenant\AiChat;
|
||||||
|
use App\Models\Tenant\AiMessage;
|
||||||
|
use App\Models\Tenant\Client;
|
||||||
|
use App\Models\Tenant\Lead;
|
||||||
|
use App\Models\Tenant\Vehicle;
|
||||||
|
use App\Models\Tenant\WorkOrder;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multi-provider AI client (Claude / GPT / Gemini). Provider + API key are
|
||||||
|
* read from tenant settings.ai. Chat history persists in ai_chats/ai_messages.
|
||||||
|
*/
|
||||||
|
class AiAssistantService
|
||||||
|
{
|
||||||
|
public function ask(AiChat $chat, string $userMessage): AiMessage
|
||||||
|
{
|
||||||
|
// Persist user message.
|
||||||
|
AiMessage::create([
|
||||||
|
'company_id' => $chat->company_id,
|
||||||
|
'ai_chat_id' => $chat->id,
|
||||||
|
'role' => 'user',
|
||||||
|
'content' => $userMessage,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$company = Company::withoutGlobalScopes()->findOrFail($chat->company_id);
|
||||||
|
$aiCfg = (array) ($company->settings['ai'] ?? []);
|
||||||
|
$provider = $chat->provider ?: ($aiCfg['default_provider'] ?? 'claude');
|
||||||
|
$apiKey = $aiCfg["{$provider}_key"] ?? null;
|
||||||
|
|
||||||
|
$start = microtime(true);
|
||||||
|
|
||||||
|
if (! $apiKey) {
|
||||||
|
$reply = '⚠️ API key pentru ' . strtoupper($provider) . ' nu e configurat. Mergi la Setări → AI și adaugă cheia.';
|
||||||
|
$meta = ['error' => 'no_api_key'];
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
[$reply, $meta] = match ($provider) {
|
||||||
|
'claude' => $this->callClaude($apiKey, $chat, $userMessage, $company),
|
||||||
|
'gpt' => $this->callOpenAI($apiKey, $chat, $userMessage, $company),
|
||||||
|
'gemini' => $this->callGemini($apiKey, $chat, $userMessage, $company),
|
||||||
|
default => ['Provider necunoscut: ' . $provider, []],
|
||||||
|
};
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$reply = '❌ Eroare API: ' . $e->getMessage();
|
||||||
|
$meta = ['error' => $e->getMessage()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$meta['latency_ms'] = (int) ((microtime(true) - $start) * 1000);
|
||||||
|
$meta['provider'] = $provider;
|
||||||
|
|
||||||
|
$msg = AiMessage::create([
|
||||||
|
'company_id' => $chat->company_id,
|
||||||
|
'ai_chat_id' => $chat->id,
|
||||||
|
'role' => 'assistant',
|
||||||
|
'content' => $reply,
|
||||||
|
'meta' => $meta,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Update chat title from first user message.
|
||||||
|
if ($chat->messages()->where('role', 'user')->count() === 1) {
|
||||||
|
$chat->update(['title' => mb_substr($userMessage, 0, 60)]);
|
||||||
|
}
|
||||||
|
$chat->touch();
|
||||||
|
|
||||||
|
return $msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function buildSystemPrompt(Company $company): string
|
||||||
|
{
|
||||||
|
$clientsCount = Client::count();
|
||||||
|
$vehiclesCount = Vehicle::count();
|
||||||
|
$newLeads = Lead::where('status', 'new')->count();
|
||||||
|
$openWO = WorkOrder::whereNotIn('status', ['done', 'cancelled'])->count();
|
||||||
|
$debtTotal = (float) WorkOrder::where('pay_status', '!=', 'paid')
|
||||||
|
->whereNotIn('status', ['cancelled'])
|
||||||
|
->get()->sum(fn ($w) => max(0, (float) $w->total - (float) $w->payments()->sum('amount')));
|
||||||
|
|
||||||
|
return <<<TXT
|
||||||
|
Ești asistent CRM pentru autoservice "{$company->name}" din {$company->city}.
|
||||||
|
Răspunzi în română (sau în limba folosită de utilizator). Fii concis, profesional, util.
|
||||||
|
|
||||||
|
Statistici curente:
|
||||||
|
- {$clientsCount} clienți, {$vehiclesCount} mașini
|
||||||
|
- {$newLeads} cereri noi neprocesate
|
||||||
|
- {$openWO} fișe de lucru active
|
||||||
|
- Total datorii clienți: {$debtTotal} MDL
|
||||||
|
|
||||||
|
Poți ajuta cu: sugestii diagnostic auto, calcul costuri, sumar performanță,
|
||||||
|
recomandări piese, redactare mesaje pentru clienți, planificare. NU inventezi
|
||||||
|
date care nu apar în context — dacă nu știi, recunoaște.
|
||||||
|
TXT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Convert chat history to provider-specific message format. */
|
||||||
|
protected function historyMessages(AiChat $chat, int $limit = 20): array
|
||||||
|
{
|
||||||
|
return $chat->messages()
|
||||||
|
->latest('created_at')
|
||||||
|
->limit($limit)
|
||||||
|
->get()
|
||||||
|
->reverse()
|
||||||
|
->values()
|
||||||
|
->map(fn (AiMessage $m) => ['role' => $m->role, 'content' => $m->content])
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function callClaude(string $key, AiChat $chat, string $msg, Company $company): array
|
||||||
|
{
|
||||||
|
$messages = $this->historyMessages($chat);
|
||||||
|
// Anthropic requires alternating user/assistant; system is separate.
|
||||||
|
$messages = array_values(array_filter($messages, fn ($m) => in_array($m['role'], ['user', 'assistant'], true)));
|
||||||
|
|
||||||
|
$r = Http::withHeaders([
|
||||||
|
'x-api-key' => $key,
|
||||||
|
'anthropic-version' => '2023-06-01',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
])
|
||||||
|
->timeout(60)
|
||||||
|
->post('https://api.anthropic.com/v1/messages', [
|
||||||
|
'model' => 'claude-sonnet-4-5',
|
||||||
|
'max_tokens' => 1024,
|
||||||
|
'system' => $this->buildSystemPrompt($company),
|
||||||
|
'messages' => $messages,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (! $r->successful()) {
|
||||||
|
return ['❌ ' . ($r->json('error.message') ?? 'Anthropic API error ' . $r->status()), ['status' => $r->status()]];
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $r->json();
|
||||||
|
$text = collect($body['content'] ?? [])
|
||||||
|
->where('type', 'text')
|
||||||
|
->pluck('text')
|
||||||
|
->implode("\n");
|
||||||
|
|
||||||
|
return [$text ?: '(răspuns gol)', [
|
||||||
|
'model' => $body['model'] ?? null,
|
||||||
|
'tokens_in' => $body['usage']['input_tokens'] ?? null,
|
||||||
|
'tokens_out' => $body['usage']['output_tokens'] ?? null,
|
||||||
|
]];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function callOpenAI(string $key, AiChat $chat, string $msg, Company $company): array
|
||||||
|
{
|
||||||
|
$messages = array_merge(
|
||||||
|
[['role' => 'system', 'content' => $this->buildSystemPrompt($company)]],
|
||||||
|
$this->historyMessages($chat),
|
||||||
|
);
|
||||||
|
|
||||||
|
$r = Http::withHeaders(['Authorization' => 'Bearer ' . $key, 'content-type' => 'application/json'])
|
||||||
|
->timeout(60)
|
||||||
|
->post('https://api.openai.com/v1/chat/completions', [
|
||||||
|
'model' => 'gpt-4o-mini',
|
||||||
|
'messages' => $messages,
|
||||||
|
'max_tokens' => 1024,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (! $r->successful()) {
|
||||||
|
return ['❌ ' . ($r->json('error.message') ?? 'OpenAI error ' . $r->status()), ['status' => $r->status()]];
|
||||||
|
}
|
||||||
|
$body = $r->json();
|
||||||
|
return [
|
||||||
|
$body['choices'][0]['message']['content'] ?? '(răspuns gol)',
|
||||||
|
[
|
||||||
|
'model' => $body['model'] ?? null,
|
||||||
|
'tokens_in' => $body['usage']['prompt_tokens'] ?? null,
|
||||||
|
'tokens_out' => $body['usage']['completion_tokens'] ?? null,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function callGemini(string $key, AiChat $chat, string $msg, Company $company): array
|
||||||
|
{
|
||||||
|
$contents = [];
|
||||||
|
foreach ($this->historyMessages($chat) as $m) {
|
||||||
|
$contents[] = [
|
||||||
|
'role' => $m['role'] === 'assistant' ? 'model' : 'user',
|
||||||
|
'parts' => [['text' => $m['content']]],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$r = Http::withHeaders(['content-type' => 'application/json'])
|
||||||
|
->timeout(60)
|
||||||
|
->post('https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=' . $key, [
|
||||||
|
'systemInstruction' => ['parts' => [['text' => $this->buildSystemPrompt($company)]]],
|
||||||
|
'contents' => $contents,
|
||||||
|
'generationConfig' => ['maxOutputTokens' => 1024],
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (! $r->successful()) {
|
||||||
|
return ['❌ Gemini error ' . $r->status() . ': ' . $r->body(), ['status' => $r->status()]];
|
||||||
|
}
|
||||||
|
$body = $r->json();
|
||||||
|
$text = $body['candidates'][0]['content']['parts'][0]['text'] ?? '(răspuns gol)';
|
||||||
|
return [$text, [
|
||||||
|
'model' => 'gemini-1.5-flash',
|
||||||
|
'tokens' => $body['usageMetadata'] ?? null,
|
||||||
|
]];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('ai_chats', function (Blueprint $t) {
|
||||||
|
$t->id();
|
||||||
|
$t->foreignId('company_id')->constrained()->cascadeOnDelete();
|
||||||
|
$t->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$t->string('title')->default('Conversație nouă');
|
||||||
|
$t->string('provider')->default('claude'); // claude / gpt / gemini
|
||||||
|
$t->timestamps();
|
||||||
|
|
||||||
|
$t->index(['company_id', 'user_id', 'updated_at']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('ai_messages', function (Blueprint $t) {
|
||||||
|
$t->id();
|
||||||
|
$t->foreignId('company_id')->constrained()->cascadeOnDelete();
|
||||||
|
$t->foreignId('ai_chat_id')->constrained()->cascadeOnDelete();
|
||||||
|
$t->string('role'); // system / user / assistant
|
||||||
|
$t->longText('content');
|
||||||
|
$t->json('meta')->nullable(); // tokens, model, latency_ms
|
||||||
|
$t->timestamps();
|
||||||
|
|
||||||
|
$t->index(['company_id', 'ai_chat_id', 'created_at']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('ai_messages');
|
||||||
|
Schema::dropIfExists('ai_chats');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
<x-filament-panels::page>
|
||||||
|
<style>
|
||||||
|
.ai-wrap { display: grid; grid-template-columns: 240px 1fr; gap: 16px; min-height: 70vh; }
|
||||||
|
@media (max-width: 768px) { .ai-wrap { grid-template-columns: 1fr; } }
|
||||||
|
.ai-side {
|
||||||
|
background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 10px;
|
||||||
|
padding: 12px; overflow-y: auto;
|
||||||
|
}
|
||||||
|
.dark .ai-side { background: #1f2937; border-color: #374151; }
|
||||||
|
.ai-new-btn {
|
||||||
|
width: 100%; padding: 8px 12px; border-radius: 6px;
|
||||||
|
background: #3b82f6; color: white; font-size: 13px; font-weight: 500;
|
||||||
|
border: none; cursor: pointer; margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
.ai-new-btn:hover { background: #2563eb; }
|
||||||
|
|
||||||
|
.ai-chat-item {
|
||||||
|
padding: 8px 10px; border-radius: 6px; margin-bottom: 4px;
|
||||||
|
font-size: 12px; cursor: pointer; display: flex; justify-content: space-between; align-items: center; gap: 6px;
|
||||||
|
}
|
||||||
|
.ai-chat-item:hover { background: #fff; }
|
||||||
|
.dark .ai-chat-item:hover { background: #374151; }
|
||||||
|
.ai-chat-item.active { background: #fff; border: 1px solid #3b82f6; }
|
||||||
|
.dark .ai-chat-item.active { background: #111827; }
|
||||||
|
.ai-chat-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
|
.ai-del { color: #9ca3af; cursor: pointer; padding: 2px 4px; border-radius: 4px; font-size: 14px; border: none; background: none; }
|
||||||
|
.ai-del:hover { color: #dc2626; background: #fef2f2; }
|
||||||
|
|
||||||
|
.ai-main {
|
||||||
|
background: #fff; border: 1px solid #e5e7eb; border-radius: 10px;
|
||||||
|
display: flex; flex-direction: column;
|
||||||
|
}
|
||||||
|
.dark .ai-main { background: #1f2937; border-color: #374151; }
|
||||||
|
|
||||||
|
.ai-messages {
|
||||||
|
flex: 1; overflow-y: auto; padding: 16px;
|
||||||
|
display: flex; flex-direction: column; gap: 12px;
|
||||||
|
min-height: 400px;
|
||||||
|
}
|
||||||
|
.ai-msg { max-width: 85%; padding: 10px 14px; border-radius: 12px; font-size: 13px; line-height: 1.5; }
|
||||||
|
.ai-msg-user {
|
||||||
|
align-self: flex-end;
|
||||||
|
background: #3b82f6; color: white;
|
||||||
|
border-bottom-right-radius: 4px;
|
||||||
|
}
|
||||||
|
.ai-msg-assistant {
|
||||||
|
align-self: flex-start;
|
||||||
|
background: #f3f4f6; color: #1f2937;
|
||||||
|
border-bottom-left-radius: 4px;
|
||||||
|
}
|
||||||
|
.dark .ai-msg-assistant { background: #374151; color: #f9fafb; }
|
||||||
|
.ai-msg pre { white-space: pre-wrap; word-wrap: break-word; margin: 0; font-family: inherit; }
|
||||||
|
.ai-msg-meta { font-size: 10px; color: #9ca3af; margin-top: 4px; }
|
||||||
|
|
||||||
|
.ai-empty { text-align: center; color: #9ca3af; padding: 60px 20px; }
|
||||||
|
|
||||||
|
.ai-form {
|
||||||
|
border-top: 1px solid #e5e7eb;
|
||||||
|
padding: 12px; display: flex; gap: 8px;
|
||||||
|
}
|
||||||
|
.dark .ai-form { border-color: #374151; }
|
||||||
|
.ai-input {
|
||||||
|
flex: 1; padding: 10px 12px; border: 1px solid #e5e7eb; border-radius: 8px;
|
||||||
|
font-size: 13px; font-family: inherit; resize: none; min-height: 40px;
|
||||||
|
}
|
||||||
|
.dark .ai-input { background: #111827; border-color: #374151; color: #f9fafb; }
|
||||||
|
.ai-send {
|
||||||
|
padding: 10px 20px; background: #3b82f6; color: white;
|
||||||
|
border: none; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 500;
|
||||||
|
}
|
||||||
|
.ai-send:hover { background: #2563eb; }
|
||||||
|
.ai-send:disabled { opacity: 0.5; cursor: not-allowed; }
|
||||||
|
|
||||||
|
.ai-loading {
|
||||||
|
align-self: flex-start;
|
||||||
|
color: #9ca3af; font-size: 13px;
|
||||||
|
padding: 10px 14px;
|
||||||
|
display: flex; align-items: center; gap: 6px;
|
||||||
|
}
|
||||||
|
.ai-loading .dot { width: 6px; height: 6px; background: #9ca3af; border-radius: 50%; animation: ai-bounce 1.4s infinite; }
|
||||||
|
.ai-loading .dot:nth-child(2) { animation-delay: 0.2s; }
|
||||||
|
.ai-loading .dot:nth-child(3) { animation-delay: 0.4s; }
|
||||||
|
@keyframes ai-bounce { 0%, 80%, 100% { opacity: 0.3; } 40% { opacity: 1; } }
|
||||||
|
</style>
|
||||||
|
|
||||||
|
@php
|
||||||
|
$chat = $this->getChat();
|
||||||
|
$chats = $this->getChats();
|
||||||
|
@endphp
|
||||||
|
|
||||||
|
<div class="ai-wrap" x-data x-init="$nextTick(() => { const m = $refs.msgs; if (m) m.scrollTop = m.scrollHeight; })">
|
||||||
|
{{-- Sidebar: chat history --}}
|
||||||
|
<div class="ai-side">
|
||||||
|
<button type="button" class="ai-new-btn" wire:click="newChat">+ Conversație nouă</button>
|
||||||
|
@forelse ($chats as $c)
|
||||||
|
<div class="ai-chat-item {{ $c->id === $chatId ? 'active' : '' }}">
|
||||||
|
<span class="ai-chat-title" wire:click="selectChat({{ $c->id }})" style="cursor:pointer;">
|
||||||
|
{{ $c->title }}
|
||||||
|
</span>
|
||||||
|
<button type="button" class="ai-del" wire:click="deleteChat({{ $c->id }})"
|
||||||
|
wire:confirm="Șterge conversația?">×</button>
|
||||||
|
</div>
|
||||||
|
@empty
|
||||||
|
<div style="font-size:11px;color:#9ca3af;padding:8px;">Nicio conversație</div>
|
||||||
|
@endforelse
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{{-- Main chat area --}}
|
||||||
|
<div class="ai-main">
|
||||||
|
<div class="ai-messages" x-ref="msgs"
|
||||||
|
x-init="$watch('$wire.loading', () => $nextTick(() => $el.scrollTop = $el.scrollHeight))">
|
||||||
|
@if (! $chat || $chat->messages->isEmpty())
|
||||||
|
<div class="ai-empty">
|
||||||
|
<div style="font-size:32px;margin-bottom:8px;">✨</div>
|
||||||
|
<div style="font-size:14px;font-weight:600;margin-bottom:4px;">Asistent AI pentru autoservice</div>
|
||||||
|
<div style="font-size:12px;">Întreabă-mă orice — diagnostic, sumar, sugestii, redactare mesaje către clienți...</div>
|
||||||
|
<div style="font-size:11px;margin-top:16px;color:#9ca3af;">
|
||||||
|
<b>Configurare:</b> mergi la Setări → Asistent AI și adaugă cheia API (Claude / GPT / Gemini).
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@else
|
||||||
|
@foreach ($chat->messages as $m)
|
||||||
|
@if ($m->role === 'user')
|
||||||
|
<div class="ai-msg ai-msg-user"><pre>{{ $m->content }}</pre></div>
|
||||||
|
@elseif ($m->role === 'assistant')
|
||||||
|
<div class="ai-msg ai-msg-assistant">
|
||||||
|
<pre>{{ $m->content }}</pre>
|
||||||
|
@if (! empty($m->meta['provider']))
|
||||||
|
<div class="ai-msg-meta">
|
||||||
|
{{ strtoupper($m->meta['provider']) }}
|
||||||
|
@if (! empty($m->meta['latency_ms'])) · {{ $m->meta['latency_ms'] }}ms @endif
|
||||||
|
@if (! empty($m->meta['tokens_out'])) · {{ $m->meta['tokens_out'] }} tok @endif
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
@endforeach
|
||||||
|
@endif
|
||||||
|
@if ($loading)
|
||||||
|
<div class="ai-loading">
|
||||||
|
<span class="dot"></span><span class="dot"></span><span class="dot"></span>
|
||||||
|
<span>asistent gândește...</span>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form wire:submit="send" class="ai-form">
|
||||||
|
<textarea
|
||||||
|
class="ai-input"
|
||||||
|
wire:model="newMessage"
|
||||||
|
placeholder="Scrie mesajul tău..."
|
||||||
|
@keydown.enter.exact.prevent="$wire.send()"
|
||||||
|
@keydown.shift.enter="$el.value += '\n'"
|
||||||
|
rows="1"
|
||||||
|
></textarea>
|
||||||
|
<button type="submit" class="ai-send" wire:loading.attr="disabled">Trimite</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-filament-panels::page>
|
||||||
Reference in New Issue
Block a user