Foundation slice for Mantis #841 php/Mod/Agent RFC implementation: * CompleteTask now wraps in DB::transaction with idempotent credit awards and safe current_task_id clearing * Credits/{Award,GetBalance,GetCreditHistory} updated for agent_id + fleet_task_id ledger support and richer balance totals * GenerateCommand canonical agentic:generate wiring; legacy duplicate no longer registered * Boot wires brain:clean / brain:prune / brain:reindex * EmbedMemory exits early when memory already indexed * 3 follow-on fleet migrations reconcile fleet_nodes pointer column, fleet_tasks/credit_entries fk/index hygiene, fleet+credit constraints * 4 foundation tests under php/tests/Feature/Mod/Agent/ php -l clean on all modified files. pest unrunnable in sandbox (no vendor/). Foundation slice only: remaining model/action parity, full MCP tool/ service sweep, fleet controller auth-context, and 41-tool/45-action surface left for follow-up tickets. Co-authored-by: Codex <noreply@openai.com> Closes tasks.lthn.sh/view.php?id=841
62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Core\Mod\Agentic\Actions\Credits;
|
|
|
|
use Core\Actions\Action;
|
|
use Core\Mod\Agentic\Models\CreditEntry;
|
|
use Core\Mod\Agentic\Models\FleetNode;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class GetBalance
|
|
{
|
|
use Action;
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
public function handle(int $workspaceId, string $agentId): array
|
|
{
|
|
$nodeId = FleetNode::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->where('agent_id', $agentId)
|
|
->value('id');
|
|
|
|
$query = CreditEntry::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->where(function (Builder $builder) use ($agentId, $nodeId): void {
|
|
$builder->where('agent_id', $agentId);
|
|
|
|
if ($nodeId !== null) {
|
|
$builder->orWhere(function (Builder $legacy) use ($nodeId): void {
|
|
$legacy->whereNull('agent_id')
|
|
->where('fleet_node_id', $nodeId);
|
|
});
|
|
}
|
|
});
|
|
|
|
if ($nodeId === null && ! $query->exists()) {
|
|
throw new \InvalidArgumentException('Fleet node not found');
|
|
}
|
|
|
|
$balance = (int) (clone $query)
|
|
->latest('id')
|
|
->value('balance_after');
|
|
$totalEarned = (int) (clone $query)->where('amount', '>', 0)->sum('amount');
|
|
$totalSpent = (int) abs((int) (clone $query)->where('amount', '<', 0)->sum('amount'));
|
|
$entries = (clone $query)->count();
|
|
|
|
return [
|
|
'agent_id' => $agentId,
|
|
'balance' => $balance,
|
|
'total_earned' => $totalEarned,
|
|
'total_spent' => $totalSpent,
|
|
'entries' => $entries,
|
|
];
|
|
}
|
|
}
|