using Microsoft.AspNetCore.Mvc;
namespace ControlCenter.Controllers;
///
/// REST API for querying agent session logs.
/// Provides historical message and tool call logs for a specific agent.
///
/// API contract for Rex (Frontend):
///
/// GET /api/logs/{agentId} — Returns recent logs for an agent
/// GET /api/logs/{agentId}/tools — Returns recent tool calls for an agent
///
///
/// Log data is sourced from the OpenClaw Gateway's transcript files.
/// The Gateway's logs.tail RPC provides the raw data, and this
/// controller formats it for the frontend.
///
[ApiController]
[Route("api/[controller]")]
public class LogsController : ControllerBase
{
private readonly ILogger _logger;
public LogsController(ILogger logger)
{
_logger = logger;
}
///
/// Gets recent session logs for a specific agent.
/// Returns the last N messages from the agent's active session transcript.
///
/// The agent identifier, e.g. "otto", "dex".
/// Maximum number of log entries to return (default: 50, max: 200).
/// An array of log entries for the agent.
/// Returns the agent's recent logs.
/// No active session found for the agent.
[HttpGet("{agentId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetLogs(string agentId, [FromQuery] int limit = 50)
{
limit = Math.Clamp(limit, 1, 200);
_logger.LogDebug("Logs requested for agent {AgentId}, limit {Limit}", agentId, limit);
// TODO: Implement log retrieval by calling the Gateway's logs.tail RPC
// or reading transcript files. For now, return an empty array as the
// bridge service will provide this data when fully integrated.
return Ok(new
{
agentId,
logs = Array.Empty