fuseboxie/sdk
Composer 安装命令:
composer require fuseboxie/sdk
包简介
Fuseboxie PHP SDK for guarding AI calls and tracking token usage from backend applications.
README 文档
README
Backend SDK for tracking AI usage and blocking AI calls when a Fuseboxie guard rule says no.
Install
composer require fuseboxie/sdk
Usage
Initialize the SDK once in your PHP backend:
<?php use function Fuseboxie\init; $fuseboxie = init([ 'projectKey' => getenv('FUSEBOXIE_PROJECT_KEY'), 'apiUrl' => getenv('FUSEBOXIE_API_URL') ?: 'https://api.fuseboxie.com', 'guardFailureMode' => 'throw', ]);
On Windows PHP installs that do not have a default CA bundle configured, pass one explicitly:
$fuseboxie = init([ 'projectKey' => getenv('FUSEBOXIE_PROJECT_KEY'), 'apiUrl' => 'https://api.fuseboxie.com', 'caBundlePath' => 'C:\\Program Files\\Git\\mingw64\\etc\\ssl\\certs\\ca-bundle.crt', ]);
Guard before the AI provider call:
$decision = $fuseboxie->canUseAI([ 'userId' => 'user_123', 'customerId' => 'customer_123', 'role' => 'trial', 'estimatedTokens' => 2000, 'estimatedCostUsd' => 0.02, 'operation' => 'chat.completion', ]); if (!$decision['allowed']) { throw new RuntimeException($decision['reason'] ?? 'AI usage is blocked.'); }
Track after the provider returns:
$response = $openai->chat()->create([...]); $fuseboxie->trackOpenAI($response, [ 'userId' => 'user_123', 'customerId' => 'customer_123', 'role' => 'trial', 'operation' => 'chat.completion', ]);
Provider helpers:
trackOpenAI($response, $context)mapsusage.prompt_tokens,usage.completion_tokens,usage.total_tokens, and the newerusage.input_tokens/usage.output_tokensshape.trackAnthropic($response, $context)mapsusage.input_tokensandusage.output_tokens.trackGemini($response, $context)mapsusageMetadata.promptTokenCount,usageMetadata.candidatesTokenCount, andusageMetadata.totalTokenCount.
You can also call trackUsage() manually when you already have normalized token counts:
$fuseboxie->trackUsage([ 'provider' => 'openai', 'model' => 'gpt-4.1-mini', 'inputTokens' => 1200, 'outputTokens' => 420, 'userId' => 'user_123', 'customerId' => 'customer_123', 'role' => 'trial', 'operation' => 'chat.completion', ]);
Laravel
The package auto-discovers a Laravel service provider and facade when installed in a Laravel app.
Publish the config file:
php artisan vendor:publish --tag=fuseboxie-config
Set your server-side environment values:
FUSEBOXIE_PROJECT_KEY=pk_live_... FUSEBOXIE_API_URL=https://api.fuseboxie.com FUSEBOXIE_GUARD_FAILURE_MODE=throw
Add the middleware to routes that make AI calls. It captures the signed-in Laravel user ID and optional customer/role/request headers, then reuses them for every Fuseboxie call in that request:
use Fuseboxie\Laravel\Middleware\FuseboxieContext; Route::middleware([FuseboxieContext::class])->post('/chat', ChatController::class);
Use the facade in your controller:
use Fuseboxie\Laravel\Facades\Fuseboxie; $decision = Fuseboxie::canUseAI([ 'estimatedTokens' => 2000, 'estimatedCostUsd' => 0.02, 'operation' => 'chat.completion', ]); if (!$decision['allowed']) { abort(402, $decision['reason'] ?? 'AI usage is blocked.'); } $response = $openai->chat()->create([...]); Fuseboxie::trackOpenAI($response, [ 'operation' => 'chat.completion', ]);
If your app stores tenant/customer IDs somewhere else, pass them directly in the guard/tracking arrays or customize the middleware header names in config/fuseboxie.php.
Checks
composer install
composer test
The SDK has no runtime dependencies beyond PHP JSON support. It uses cURL when available and falls back to PHP streams.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-11