This repository has been archived on 2026-03-09. You can view files and clone it, but cannot push or open issues or pull requests.
php-agentic/Actions/Session/ListSessions.php
Snider 6f0618692a
Some checks failed
CI / PHP 8.3 (push) Failing after 2s
CI / PHP 8.4 (push) Failing after 2s
feat: add plan/session/phase/task Actions + slim MCP tools
Extract business logic from MCP tool handlers into 15 Action classes
(Plan 5, Session 5, Phase 3, Task 2) following the Brain pattern.
MCP tools become thin wrappers calling Action::run(). Add framework-level
REST controllers and routes as sensible defaults for consumers.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-04 13:58:45 +00:00

68 lines
1.8 KiB
PHP

<?php
/*
* Core PHP Framework
*
* Licensed under the European Union Public Licence (EUPL) v1.2.
* See LICENSE file for details.
*/
declare(strict_types=1);
namespace Core\Mod\Agentic\Actions\Session;
use Core\Actions\Action;
use Core\Mod\Agentic\Models\AgentSession;
use Core\Mod\Agentic\Services\AgentSessionService;
use Illuminate\Support\Collection;
/**
* List sessions for a workspace, with optional filtering.
*
* Usage:
* $sessions = ListSessions::run(1);
* $sessions = ListSessions::run(1, 'active', 'deploy-v2', 20);
*/
class ListSessions
{
use Action;
public function __construct(
private AgentSessionService $sessionService,
) {}
/**
* @return Collection<int, AgentSession>
*/
public function handle(int $workspaceId, ?string $status = null, ?string $planSlug = null, ?int $limit = null): Collection
{
if ($status !== null) {
$valid = ['active', 'paused', 'completed', 'failed'];
if (! in_array($status, $valid, true)) {
throw new \InvalidArgumentException(
sprintf('status must be one of: %s', implode(', ', $valid))
);
}
}
// Active sessions use the optimised service method
if ($status === 'active' || $status === null) {
return $this->sessionService->getActiveSessions($workspaceId);
}
$query = AgentSession::query()
->where('workspace_id', $workspaceId)
->where('status', $status)
->orderBy('last_active_at', 'desc');
if ($planSlug !== null) {
$query->whereHas('plan', fn ($q) => $q->where('slug', $planSlug));
}
if ($limit !== null && $limit > 0) {
$query->limit(min($limit, 1000));
}
return $query->get();
}
}