Align POS header with other apps: AI assistant, register button, sidebar
Deploy Ladill POS / deploy (push) Successful in 47s
Deploy Ladill POS / deploy (push) Successful in 47s
- Add the Afia AI assistant to the header (topbar AI button + slide-over), matching every other Ladill app. Ports config/afia.php, AfiaService, a POS AiController (pos.ai.chat) scoped to register/sales/products, and includes the slide-over in the app layout. The afia() Alpine component already existed. - Hide the header "Open register" button while on the register page. - Move Settings to a pinned bottom section of the sidebar, like other apps. - Commit the updated POS logo + launcher icon assets. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
b7c7a32ee6
commit
e9a0c92308
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Pos;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Pos\Concerns\ScopesToAccount;
|
||||
use App\Models\PosProduct;
|
||||
use App\Models\PosSale;
|
||||
use App\Services\Afia\AfiaService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AiController extends Controller
|
||||
{
|
||||
use ScopesToAccount;
|
||||
|
||||
public function chat(Request $request, AfiaService $afia): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'message' => ['required', 'string', 'max:2000'],
|
||||
'history' => ['nullable', 'array', 'max:20'],
|
||||
'history.*.role' => ['nullable', 'string'],
|
||||
'history.*.text' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
if (! $afia->enabled()) {
|
||||
return response()->json(['message' => 'Afia is not available right now.'], 503);
|
||||
}
|
||||
|
||||
try {
|
||||
$reply = $afia->chat(trim($validated['message']), $validated['history'] ?? [], $this->context($request));
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
|
||||
return response()->json(['message' => 'Afia could not respond right now. Please try again.'], 502);
|
||||
}
|
||||
|
||||
return response()->json(['reply' => $reply]);
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
private function context(Request $request): array
|
||||
{
|
||||
$owner = $this->ownerRef($request);
|
||||
$currency = strtoupper((string) config('pos.default_currency', 'GHS'));
|
||||
|
||||
$todaySales = PosSale::owned($owner)
|
||||
->where('status', PosSale::STATUS_PAID)
|
||||
->where('paid_at', '>=', now()->startOfDay());
|
||||
|
||||
$todayTotalMinor = (int) (clone $todaySales)->sum('total_minor');
|
||||
|
||||
return [
|
||||
'signed_in' => 'yes',
|
||||
'today_takings' => $currency.' '.number_format($todayTotalMinor / 100, 2),
|
||||
'today_sales_count' => (clone $todaySales)->count(),
|
||||
'active_products' => PosProduct::owned($owner)->active()->count(),
|
||||
'pending_sales' => PosSale::owned($owner)->where('status', PosSale::STATUS_PENDING)->count(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Afia;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Afia — Ladill in-app AI assistant scoped to POS. Calls the configured
|
||||
* provider (OpenAI/Anthropic) directly with the app's own key.
|
||||
*/
|
||||
class AfiaService
|
||||
{
|
||||
public function enabled(): bool
|
||||
{
|
||||
return (bool) config('afia.enabled', true) && (string) config('afia.api_key', '') !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int,array{role:string,text:string}> $history
|
||||
* @param array<string,mixed> $context
|
||||
*/
|
||||
public function chat(string $message, array $history, array $context): string
|
||||
{
|
||||
if (! $this->enabled()) {
|
||||
throw new RuntimeException('Afia is not configured.');
|
||||
}
|
||||
|
||||
$provider = (string) config('afia.provider', 'openai');
|
||||
$model = (string) config('afia.model', 'gpt-4o-mini');
|
||||
$apiKey = (string) config('afia.api_key');
|
||||
|
||||
$messages = [['role' => 'system', 'content' => $this->systemPrompt($context)]];
|
||||
foreach (array_slice($history, -8) as $turn) {
|
||||
$role = ($turn['role'] ?? 'user') === 'assistant' ? 'assistant' : 'user';
|
||||
$text = trim((string) ($turn['text'] ?? ''));
|
||||
if ($text !== '') {
|
||||
$messages[] = ['role' => $role, 'content' => $text];
|
||||
}
|
||||
}
|
||||
$messages[] = ['role' => 'user', 'content' => $message];
|
||||
|
||||
return $provider === 'anthropic'
|
||||
? $this->viaAnthropic($model, $apiKey, $messages)
|
||||
: $this->viaOpenAi($model, $apiKey, $messages);
|
||||
}
|
||||
|
||||
private function viaOpenAi(string $model, string $apiKey, array $messages): string
|
||||
{
|
||||
$res = Http::withToken($apiKey)->acceptJson()->timeout(45)
|
||||
->post('https://api.openai.com/v1/chat/completions', [
|
||||
'model' => $model,
|
||||
'temperature' => 0.3,
|
||||
'max_tokens' => 600,
|
||||
'messages' => $messages,
|
||||
]);
|
||||
|
||||
if ($res->failed()) {
|
||||
throw new RuntimeException('OpenAI request failed: '.$res->status());
|
||||
}
|
||||
|
||||
return trim((string) $res->json('choices.0.message.content', ''));
|
||||
}
|
||||
|
||||
private function viaAnthropic(string $model, string $apiKey, array $messages): string
|
||||
{
|
||||
$system = $messages[0]['content'];
|
||||
$turns = array_values(array_filter($messages, fn ($m) => $m['role'] !== 'system'));
|
||||
|
||||
$res = Http::withHeaders([
|
||||
'x-api-key' => $apiKey,
|
||||
'anthropic-version' => '2023-06-01',
|
||||
])->acceptJson()->timeout(45)->post('https://api.anthropic.com/v1/messages', [
|
||||
'model' => $model,
|
||||
'max_tokens' => 600,
|
||||
'system' => $system,
|
||||
'messages' => array_map(fn ($m) => ['role' => $m['role'], 'content' => $m['content']], $turns),
|
||||
]);
|
||||
|
||||
if ($res->failed()) {
|
||||
throw new RuntimeException('Anthropic request failed: '.$res->status());
|
||||
}
|
||||
|
||||
return trim((string) $res->json('content.0.text', ''));
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $context */
|
||||
private function systemPrompt(array $context): string
|
||||
{
|
||||
$ctx = collect($context)->map(fn ($v, $k) => "- {$k}: {$v}")->implode("\n");
|
||||
|
||||
return <<<PROMPT
|
||||
You are Afia, the assistant inside Ladill POS (pos.ladill.com).
|
||||
Help users run the in-store register — taking sales, charging by cash or Ladill Pay, managing the product catalog, and reading their sales history. Be concise, friendly, and actionable: short numbered steps and name the screen or section to use.
|
||||
|
||||
What Ladill POS does and where things live:
|
||||
- Overview (Dashboard): today's takings, number of sales, active products, and recent sales.
|
||||
- Register: the point-of-sale screen — add catalog products or a quick custom amount to the cart, then charge by cash or Ladill Pay (MoMo/card).
|
||||
- Products: the local catalog (name, SKU, price). Products can also be imported from Ladill CRM in Settings.
|
||||
- Sales: history of completed sales with receipts; a paid sale can be turned into a Ladill Invoice.
|
||||
- Settings: store details, default currency, and importing products from CRM or the Merchant storefront.
|
||||
|
||||
Rules:
|
||||
- Only answer questions about Ladill POS — the register, sales, products, checkout (cash/Ladill Pay), receipts, and settings.
|
||||
- If asked about contacts, invoices, domains, hosting, email mailboxes, or QR codes, briefly say those live in other Ladill apps and suggest the app launcher.
|
||||
- Never invent figures (takings, counts) — use the user context below or tell them where to check.
|
||||
|
||||
Current user context:
|
||||
{$ctx}
|
||||
PROMPT;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user