symfony/ai-pogocache-message-store
Symfony AI Chat integration for Pogocache message storage. Persist and retrieve chat messages via Pogocache’s HTTP API with simple configuration, enabling shared, durable conversation history backed by Pogocache and compatible with its authentication options.
Install the Package:
composer require symfony/ai-pogocache-message-store
Ensure symfony/ai-chat (≥0.9) and symfony/http-client (≥7.3) are installed.
Configure Pogocache:
Add credentials to .env:
POGOCACHE_URL=https://your-pogocache-instance.com/api
POGOCACHE_TOKEN=your_api_token_here
Bind the Message Store:
Register the store in config/app.php or a service provider:
$app->bind(
\Symfony\Component\Ai\Chat\MessageStoreInterface::class,
function ($app) {
return new \Symfony\Ai\PogocacheMessageStore(
$app->make(\Symfony\Contracts\HttpClient\HttpClientInterface::class),
$app->make(\Symfony\Component\Serializer\SerializerInterface::class),
$app['config']['services.pogocache.token']
);
}
);
First Use Case:
Use Symfony AI Chat’s Chat class to send/retrieve messages:
use Symfony\Component\Ai\Chat\Chat;
use Symfony\Component\Ai\Chat\Message;
$chat = new Chat($messageStore);
$chat->addMessage(new Message('user', 'Hello!'));
$response = $chat->ask('ai', 'How are you?');
Message Persistence:
Chat class.$message = $messageStore->getMessage('chat_id:123');
Laravel Integration:
public function register(): void {
$this->app->singleton(\Symfony\Component\Ai\Chat\MessageStoreInterface::class, function ($app) {
return new \Symfony\Ai\PogocacheMessageStore(
$app->make(\Symfony\Contracts\HttpClient\HttpClientInterface::class),
new \Symfony\Component\Serializer\Serializer(),
config('services.pogocache.token')
);
});
}
Event-Driven Patterns:
$messageStore->addListener(
\Symfony\Component\Ai\Chat\Events\MessageStored::class,
function ($event) {
event(new \App\Events\ChatMessageStored($event->getMessage()));
}
);
Queue Integration:
ChatMessage::created(function ($message) {
StorePogocacheMessage::dispatch($message);
});
HTTP Client Abstraction:
HttpClient with Laravel’s HttpClient:
use Illuminate\Support\Facades\Http;
class LaravelPogocacheStore {
public function save(Message $message): void {
Http::withHeaders([
'Authorization' => 'Bearer '.config('services.pogocache.token'),
'Content-Type' => 'application/json',
])->post(config('services.pogocache.url'), [
'json' => $message->toArray(),
]);
}
}
Fallback Mechanism:
public function getMessage(string $id): ?Message {
return cache()->remember("pogocache:fallback:{$id}", now()->addMinutes(10), function () use ($id) {
return $this->pogocache->fetch($id);
});
}
Model Binding:
class ChatMessage extends Model {
protected $casts = ['content' => 'array'];
public static function boot(): void {
static::saved(function ($model) {
app(\Symfony\Component\Ai\Chat\MessageStoreInterface::class)->save($model);
});
}
}
Testing:
Http::fake([
config('services.pogocache.url') => Http::response(['content' => 'test'], 200),
]);
Serialization Mismatches:
Serializer may not handle Laravel’s Carbon instances or custom objects.$data = [
'content' => $message->content,
'created_at' => $message->created_at->toIso8601String(),
];
HTTP Client Timeouts:
HttpClient with middleware:
Http::withOptions([
'timeout' => 5.0,
'connect_timeout' => 2.0,
])->retry(3, 100);
Idempotency:
PUT for updates and check for existence:
if (!$this->exists($message->id)) {
$this->save($message);
}
Token Management:
POGOCACHE_TOKEN=${VAULT_SECURE_POGOCACHE_TOKEN}
Rate Limiting:
use Symfony\Component\HttpClient\Retry\RetryStrategy;
$client = Http::withOptions([
'retry_strategy' => new RetryStrategy(3, 100, true),
]);
Log HTTP Traffic:
Http::macro('pogocache', function ($callback) {
$response = Http::withHeaders([
'Authorization' => 'Bearer '.config('services.pogocache.token'),
])->toPogocache($callback);
\Log::debug('Pogocache Request', [
'url' => $response->originalRequest()->url(),
'method' => $response->originalRequest()->method(),
'response' => $response->body(),
]);
return $response;
});
Validate Payloads:
Validator to ensure messages conform to Pogocache’s schema:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($message->toArray(), [
'content' => 'required|string|max:10000',
'created_at' => 'required|date',
]);
Monitor Performance:
\Log::channel('telescope')->info('Pogocache Miss', [
'message_id' => $message->id,
'fallback_used' => true,
]);
Custom Serialization:
$serializer = new \Symfony\Component\Serializer\Serializer([
new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer(),
new \App\Serializer\CarbonNormalizer(), // Custom normalizer
]);
Message Deduplication:
class DeduplicatePogocacheMessages implements ShouldQueue {
public function handle(): void {
$messages = Message::whereNull('pogocache_id')->get();
foreach ($messages as $message) {
$pogocacheId = $this->store->save($message);
$message->update(['pogocache_id' => $pogocacheId]);
}
}
}
Multi-Region Support:
public function getPogocacheUrl(): string {
$region = request()->ip() === 'eu' ? 'eu' : 'us';
return "https://pogocache-{$region}.com/api";
}
Analytics Integration:
$messageStore->addListener(
\Symfony\Component\Ai\Chat\Events\MessageRead::class,
function ($event) {
\App\Models\Analytics
How can I help you explore Laravel packages today?