- Fix model_pricing table references (model_id -> model, display_name -> model)
- Fix price columns (output_price_per_1k -> output_price_per_million)
- Add price conversion (per_million / 1000 = per_1k) in all API responses
- Add whereNotNull('model') filters to exclude invalid entries
- Add getModelDisplayName() helper method to all controllers
- Fix AccountController to use gateway_users budget fields directly
- Remove Budget model dependencies from AccountController
- Add custom Scramble server URL configuration for API docs
- Create ScrambleServiceProvider to set correct /api prefix
- Add migration to rename user_id to gateway_user_id in llm_requests
- Add custom ApiGuard for gateway_users authentication
- Update all API controllers: AccountController, ModelController, PricingController, ProviderController
All API endpoints now working correctly:
- GET /api/account
- GET /api/models
- GET /api/pricing
- GET /api/providers/{provider}
87 lines
3.0 KiB
PHP
87 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\UsageLog;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class LogLlmRequest implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $timeout = 30;
|
|
public int $tries = 3;
|
|
public int $maxExceptions = 3;
|
|
|
|
public function __construct(
|
|
private string $userId, // Changed from int to string for gateway_user_id
|
|
private string $provider,
|
|
private string $model,
|
|
private array $requestPayload,
|
|
private ?array $responsePayload,
|
|
private int $promptTokens,
|
|
private int $completionTokens,
|
|
private int $totalTokens,
|
|
private ?int $responseTimeMs,
|
|
private float $promptCost,
|
|
private float $completionCost,
|
|
private float $totalCost,
|
|
private string $status,
|
|
private ?string $errorMessage = null,
|
|
private ?int $httpStatus = null,
|
|
private ?string $ipAddress = null,
|
|
private ?string $userAgent = null,
|
|
private ?string $requestId = null
|
|
) {}
|
|
|
|
public function handle(): void
|
|
{
|
|
try {
|
|
UsageLog::create([
|
|
'request_id' => $this->requestId,
|
|
'gateway_user_id' => $this->userId, // Changed from user_id
|
|
'provider' => $this->provider,
|
|
'model' => $this->model,
|
|
'request_payload' => $this->requestPayload,
|
|
'response_payload' => $this->responsePayload,
|
|
'prompt_tokens' => $this->promptTokens,
|
|
'completion_tokens' => $this->completionTokens,
|
|
'total_tokens' => $this->totalTokens,
|
|
'response_time_ms' => $this->responseTimeMs,
|
|
'cost' => $this->totalCost, // UsageLog has single 'cost' field
|
|
'status' => $this->status,
|
|
'error_message' => $this->errorMessage,
|
|
'ip_address' => $this->ipAddress,
|
|
'user_agent' => $this->userAgent,
|
|
'timestamp' => now(), // UsageLog uses 'timestamp' instead of created_at
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Failed to log LLM request to UsageLog', [
|
|
'error' => $e->getMessage(),
|
|
'gateway_user_id' => $this->userId,
|
|
'provider' => $this->provider,
|
|
'model' => $this->model,
|
|
'request_id' => $this->requestId,
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function failed(\Throwable $exception): void
|
|
{
|
|
Log::critical('LogLlmRequest job failed after all retries', [
|
|
'gateway_user_id' => $this->userId,
|
|
'provider' => $this->provider,
|
|
'model' => $this->model,
|
|
'request_id' => $this->requestId,
|
|
'error' => $exception->getMessage(),
|
|
]);
|
|
}
|
|
}
|