AgentSea

Reference

AgentSea API reference

REST routes, Server-Sent Events, WebSocket events, authentication, rate limiting and error handling for the NestJS integration.

01Overview

This page covers the REST, Server-Sent Events and WebSocket routes served by the NestJS integration.

AgenticModule.forRoot registers the REST controller and the WebSocket gateway. Set enableRestApi: false or enableWebSocket: false to leave out either one.

02REST endpoints

The REST routes are enabled with enableRestApi. Successful responses use the envelope { success, data }. The execute route also returns conversationId beside data, and the delete route returns message in place of data.

GET /agents
List the registered agents. See section 03.
GET /agents/:name
Get one agent's configuration. See section 04.
POST /agents/:name/execute
Run an agent on one input. See section 05.
DELETE /agents/:name/conversations/:conversationId
Report a conversation as deleted. See section 06.
GET /agents/:name/stream
Stream one run as Server-Sent Events. See section 09.

03List agents

  • Each item in data has name, description, model, provider and tools (the tool names).
HTTP
GET /agents HTTP/1.1
Host: localhost:3000

04Get an agent

  • data has name, description, model, provider, systemPrompt, tools (name and description for each), temperature, maxTokens and maxIterations.
  • An unknown name throws an error. See Error handling.
HTTP
GET /agents/customer-support HTTP/1.1
Host: localhost:3000

05Execute an agent

  • Body fields (ExecuteAgentDto): input (required), conversationId, userId, sessionData, history, metadata, outputFormat and formatOptions.
  • data is the agent response: content, toolCalls, metadata (tokensUsed, latencyMs, iterations, cost) and finishReason.
HTTP
POST /agents/customer-support/execute HTTP/1.1
Host: localhost:3000
Content-Type: application/json

{
  "input": "I need help with my order #12345",
  "conversationId": "conv-user-123",
  "userId": "user-123",
  "sessionData": { "orderNumber": "12345" },
  "outputFormat": "markdown"
}

Note

When conversationId is omitted, the controller uses conv-<timestamp>. outputFormat and formatOptions change the shared agent for later requests too. See Formatting.

06Delete a conversation

The response is { success: true, message: 'Conversation conv-user-123 deleted' }.

HTTP
DELETE /agents/customer-support/conversations/conv-user-123 HTTP/1.1
Host: localhost:3000

Warning

The handler does not touch any memory store. It reports success, and history stays in place. Do not rely on this route to delete data.

07Server-Sent Events

The stream route sends each agent event as a message whose data is the event object as JSON. Events arrive in the order the agent produces them.

TEXT
data: {"type":"iteration","iteration":1}

data: {"type":"content","content":"I'd be happy to","delta":true}

data: {"type":"done","metadata":{"tokensUsed":450,"iterations":1}}

08SSE event types

iteration
iteration (number)
content
content (string), delta (true on streaming chunks)
tool_calls
toolCalls (array of id, tool, parameters)
tool_result
toolCall (id, tool, parameters, result)
done
metadata (tokensUsed, iterations)
error
error (string)

09Stream route method

The stream route is declared with @Sse, which NestJS registers as GET. The handler reads input from the request body, but a browser EventSource cannot send a body. Check the method and body your client uses before you build on it.

HTTP
GET /agents/customer-support/stream HTTP/1.1
Host: localhost:3000
Accept: text/event-stream
Content-Type: application/json

{"input": "Hello!"}

Note

The route is registered as GET only. No POST route streams events. Test the request with the client you plan to use before you publish a client.

10WebSocket

The gateway serves the /agents namespace. Connect with Socket.IO. The gateway allows any origin ('*'). Restrict it before deploying.

TS
import { io } from 'socket.io-client';

const socket = io('http://localhost:3000/agents');
let text = '';

socket.on('stream', (event) => {
  if (event.type === 'content') text += event.content;
});
socket.on('complete', ({ conversationId }) => console.log(conversationId, text));
socket.on('error', ({ message }) => console.error(message));
socket.emit('execute', { agentName: 'customer-support', input: 'I need help with my order' });

11Client events

execute
agentName, input (required); conversationId, userId, sessionData, history, metadata (optional)
getAgent
agentName
listAgents
none

12Server events

stream
An agent event, with the same shapes as the SSE types
complete
{ conversationId }
agentInfo
{ name, description, model, provider, tools }. Here tools is a list of { name, description }.
agentList
{ agents: [{ name, description, model, provider, tools }] }. Here tools is a list of names.
error
{ message }

13Authentication

Neither the REST routes nor the WebSocket gateway apply a guard by default. Requests reach them without authentication unless your app adds guards.

The package includes TenantGuard, which reads the API key from x-api-key and the tenant from x-tenant-id by default. It needs a tenant setup, described in the multi-tenancy guide.

TS
import { Controller, UseGuards } from '@nestjs/common';
import { TenantGuard } from '@lov3kaizen/agentsea-nestjs';

@Controller('agents')
@UseGuards(TenantGuard)
export class ProtectedAgentsController {}

Warning

The WebSocket handlers have no guards. Add guards to execute and the other handlers before exposing them.

14Rate limiting

Use RateLimit on a route and RateLimitGuard on the class or method. RateLimit takes maxRequests, windowMs and an optional keyExtractor. The default key is the request IP. A request over the limit gets HTTP 429 with the message 'Rate limit exceeded'. The built-in routes do not apply the guard.

TS
import { Controller, Param, Post, UseGuards } from '@nestjs/common';
import { RateLimit, RateLimitGuard } from '@lov3kaizen/agentsea-nestjs';

@Controller('custom-agents')
export class CustomAgentsController {
  @Post(':name/execute')
  @UseGuards(RateLimitGuard)
  @RateLimit({ maxRequests: 10, windowMs: 60000 })
  run(@Param('name') name: string) {
    return { name };
  }
}

Note

The rate limiter options are maxRequests and windowMs.

15Error handling

Unknown agent
GET /agents/:name, POST /agents/:name/execute, the stream route
Rate limit exceeded
Routes with RateLimitGuard
Run fails, SSE
Stream route
Unknown agent, WebSocket
execute, getAgent
Run fails, WebSocket
execute

Warning

Errors are thrown as plain errors. Nest returns HTTP 500 unless the app adds an exception filter. Add a filter if you need a custom error body.

16Request validation

The DTOs use class-validator decorators. Whether invalid bodies are rejected depends on a validation pipe in your app. The package does not set up a validation pipe. Confirm how your app handles invalid bodies.