AgentSea

Guide

NestJS

Add AgentSea to a NestJS app with AgenticModule, optional REST and WebSocket routes, and decorator-based agents.

01Install

The NestJS package re-exports everything from the core package, so one import path covers both.

BASH
pnpm add @lov3kaizen/agentsea-nestjs @lov3kaizen/agentsea-core

02Register the module

AgenticModule.forRoot takes provider ('anthropic', 'openai' or an LLMProvider instance), apiKey, memory, defaultAgentConfig, enableRestApi and enableWebSocket.

TS
import { Module } from '@nestjs/common';
import { AgenticModule } from '@lov3kaizen/agentsea-nestjs';

@Module({
  imports: [
    AgenticModule.forRoot({
      provider: 'anthropic',
      apiKey: process.env.ANTHROPIC_API_KEY,
      defaultAgentConfig: { model: 'claude-sonnet-4-6', provider: 'anthropic' },
      enableRestApi: true,
      enableWebSocket: true,
    }),
  ],
})
export class AppModule {}

Note

Pass the option as defaultAgentConfig. The module accepts it, but nothing in the package reads it yet. The REST and WebSocket routes serve only agents registered with AgentService.registerAgent(agent), so register your agents there.

03REST routes

With enableRestApi set, AgentController serves these routes under /agents:

  • GET /agents: lists agents.
  • GET /agents/:name: returns one agent, including its system prompt.
  • POST /agents/:name/execute: runs an agent and returns the full response.
  • DELETE /agents/:name/conversations/:conversationId: returns success. It does not delete any history yet.
  • /agents/:name/stream: streams agent events over Server-Sent Events. See the API reference.

Warning

The DELETE route returns success without deleting history. Do not rely on it to remove data yet.

04WebSocket gateway

With enableWebSocket set, AgentGateway serves the /agents Socket.IO namespace. Clients send execute, getAgent and listAgents. The server replies with stream, complete, agentInfo, agentList and error.

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

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

socket.emit('listAgents');
socket.on('agentList', (data) => console.log(data.agents));

socket.emit('execute', { agentName: 'assistant', input: 'Hello!' });
socket.on('stream', (event) => {
  if (event.type === 'content') console.log(event.content);
});
socket.on('complete', (data) => console.log(data.conversationId));

Warning

The gateway sets cors origin to '*'. Restrict it before you deploy.

05Decorator-defined agents

The package exports AgentDecorator and ToolDecorator. The decorators are named this way because the core package already exports Agent and Tool.

TS
import { Injectable } from '@nestjs/common';
import { AgentDecorator, ToolDecorator } from '@lov3kaizen/agentsea-nestjs';

@Injectable()
@AgentDecorator({ name: 'assistant', description: 'A helpful assistant', model: 'claude-sonnet-4-6', provider: 'anthropic' })
export class AssistantAgent {
  @ToolDecorator({ name: 'greet', description: 'Greet a user by name' })
  greet(params: { name: string }) {
    return `Hello, ${params.name}!`;
  }
}

Warning

The decorators only attach metadata. Nothing in the package reads it, so a class marked this way is not registered as an agent and its tool is not added to the agent. Build the agent in code and pass it to AgentService.registerAgent. The @Tool decorator takes name and description only.

06Rate limiting

RateLimit stores its options on a method. RateLimitGuard reads them and throws HTTP 429 with the message 'Rate limit exceeded' when a key is over its limit. The default key is the request IP. The built-in routes do not use the guard, so add it to the routes you want limited.

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

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

07Tenants

The package exports TenantGuard, TenantId, Tenant and TenantController. Setup is described in the multi-tenancy guide.

Warning

TenantController has no guard. Protect its routes before exposing them.