Files
hado/core/app/Mcp/Tools/AgentTool.php

51 lines
1.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Mcp\Tools;
use App\Ingest\ManualEvents;
use App\Models\Event;
use App\Models\User;
use Closure;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\ResponseFactory;
use Laravel\Mcp\Server\Tool;
/** Общее для тулов агента: пользователь токена и его события. */
abstract class AgentTool extends Tool
{
/** Пользователь, которого положил AuthenticateAgent — тот же атрибут, что у RemoteUser. */
protected function user(): User
{
return request()->attributes->get('user');
}
/** Событие по `id` из аргументов; чужое или несуществующее — ошибка для модели, а не 500. */
protected function withEvent(Request $request, Closure $then): Response|ResponseFactory
{
$id = (int) $request->validate(['id' => ['required', 'integer']])['id'];
try {
$event = $this->user()->ownEvent($id);
} catch (ModelNotFoundException) {
return Response::error('Событие не найдено');
}
return $then($event);
}
/** То же, но только своё (manual) событие — для правки и снятия. */
protected function withOwnEvent(Request $request, Closure $then): Response|ResponseFactory
{
return $this->withEvent($request, fn (Event $event) => ManualEvents::owns($event)
? $then($event)
: Response::error("Это событие источника {$event->source->name}: правит и снимает его только источник. Можно закрыть через mark_done."));
}
/** @param array<string,mixed> $data */
protected function respond(array $data): ResponseFactory
{
return Response::structured($data);
}
}