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(), count = 0, hasMore = false }); } /// /// Gets recent tool call logs for a specific agent. /// Returns the last N tool invocations from the agent's session. /// /// The agent identifier. /// Maximum number of tool entries to return (default: 20, max: 100). /// An array of tool call entries for the agent. /// Returns the agent's recent tool calls. /// No active session found for the agent. [HttpGet("{agentId}/tools")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public IActionResult GetToolLogs(string agentId, [FromQuery] int limit = 20) { limit = Math.Clamp(limit, 1, 100); _logger.LogDebug("Tool logs requested for agent {AgentId}, limit {Limit}", agentId, limit); // TODO: Implement tool log retrieval. Return empty for now. return Ok(new { agentId, tools = Array.Empty(), count = 0, hasMore = false }); } }