Eventos del SDK
El SDK emite eventos globales que puedes escuchar para monitorear el estado:Metodos
| Metodo | Descripcion |
|---|---|
on(event, handler) | Registra un listener |
off(event, handler) | Elimina un listener |
once(event, handler) | Listener que se ejecuta una sola vez |
Tabla de eventos
| Evento | Handler | Cuando se emite |
|---|---|---|
error | (error: ThaliqError) => void | Error no manejado |
rateLimit | (error: ThaliqError & { retryAfter }) => void | Rate limit alcanzado |
retry | (attempt, error, delayMs) => void | Antes de un reintento automatico |
stream.start | (conversationId) => void | Stream SSE iniciado |
stream.end | (conversationId) => void | Stream SSE finalizado |
identify | (userId: string) => void | identify() llamado |
reset | () => void | reset() llamado |
Eventos SSE del stream
Al consumir un stream confor await, cada evento tiene un campo type que identifica su contenido. La tabla completa de tipos:
Eventos de contenido
| Tipo | Descripcion | Campos |
|---|---|---|
meta | Metadata inicial | conversationId: string |
status | Estado del agente | text: string |
content.delta | Fragmento de texto | delta: string |
Eventos de tools
| Tipo | Descripcion | Campos |
|---|---|---|
tool.start | Tool comienza a ejecutarse | tool: string |
tool.end | Tool termino | tool: string, success: boolean |
Eventos interactivos
| Tipo | Descripcion | Campos |
|---|---|---|
action | Accion HITL requerida | action: PendingAction |
handoff | Escalado a agente humano | message: string, agentName?: string, reason?: string |
Eventos de finalizacion
| Tipo | Descripcion | Campos |
|---|---|---|
response.completed | Respuesta completa | message, conversationId, insights[], metadata |
message_stop | Generacion finalizada | model: string, usage: { inputTokens, outputTokens } |
Eventos de error y control
| Tipo | Descripcion | Campos |
|---|---|---|
rate_limit | Rate limit excedido | message: string, retryAfter: number |
error | Error durante el stream | message: string |
keepalive | Ping para mantener conexion | ts: number |
Ejemplo completo de manejo de eventos
Manejo de errores
El SDK expone errores tipados para un manejo granular:Clases de error
| Clase | HTTP Status | Descripcion |
|---|---|---|
ThaliqError | Base | Error base del SDK |
AuthError | 401 / 403 | API Key invalida o plan insuficiente |
RateLimitError | 429 | Rate limit alcanzado (incluye retryAfter) |
ValidationError | 400 | Request invalido (ej: falta apiKey) |
ServiceError | 503 | Servicio no disponible |
StreamError | — | Error durante streaming SSE |
TimeoutError | — | Request timeout (configurable) |
ConnectionError | — | Error de red |
Propiedades de ThaliqError
Codigos de error
| Codigo | Descripcion |
|---|---|
AUTH_ERROR | Autenticacion fallida |
RATE_LIMIT | Rate limit excedido |
VALIDATION_ERROR | Request invalido |
SERVICE_ERROR | Servicio no disponible |
STREAM_ERROR | Error en streaming |
TIMEOUT_ERROR | Timeout de request |
CONNECTION_ERROR | Error de conexion |
UNKNOWN_ERROR | Error no clasificado |
RateLimitError
StreamError
Error especifico del streaming. Se lanza cuando:- El servidor envia un evento de error
- Se intenta consumir un stream mas de una vez
- El rate limit se excede durante el stream
Errores en SSE vs excepciones
Hay dos formas en que pueden llegar errores durante streaming:| Mecanismo | Cuando | Como manejarlo |
|---|---|---|
Evento error | El agente emite un error controlado | if (event.type === 'error') dentro del loop |
Evento rate_limit | Se excedio el rate limit del tenant | if (event.type === 'rate_limit') dentro del loop |
Excepcion StreamError | Error de red, stream corrupto | try/catch alrededor del for await |
Excepcion RateLimitError | Rate limit en chat() (sin stream) | try/catch alrededor de chat() |

