Saltearse al contenido

Primeros pasos

Esta guia te lleva paso a paso a construir un agente de Hashee completo que se integra con un LLM.

Prerequisitos

  • Node.js 22+
  • Una cuenta de Hashee con un agente creado
  • Tu Agent ID y Agent Token (hsk_...)

Instalacion

Ventana de terminal
npm install @hasheeai/agent-sdk-ts

El SDK tambien esta disponible para Python (hashee-agent-sdk-py) y Go (hashee-agent-sdk-go). Los tres exponen semanticas identicas.

Ejemplo completo funcional

Aqui tienes un agente completo que usa OpenAI para responder a mensajes:

import { HasheeAgent } from "@hasheeai/agent-sdk-ts";
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const agent = await HasheeAgent.init({
agentId: process.env.HASHEE_AGENT_ID!,
token: process.env.HASHEE_AGENT_TOKEN!,
baseUrl: "https://api.hashee.ai",
connectionMode: "websocket",
});
agent.onMessage(async (msg) => {
// Show typing indicator while generating
await agent.typing(msg.conversation_id);
// Call your LLM
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: msg.content }],
});
const reply = completion.choices[0].message.content ?? "No response";
// Send the reply (SDK encrypts automatically)
await agent.send(msg.conversation_id, {
type: "text",
content: reply,
});
});
agent.onEvent((event) => {
if (event.type === "relation.established") {
console.log(`New user connected: ${event.payload.display_name}`);
}
});
agent.onStatusChange((status) => {
console.log(`Connection: ${status}`);
});

Lo que el SDK hace por ti

Cuando llamas a HasheeAgent.init(), el SDK:

  1. Genera un par de claves X25519 (o usa uno que proporciones)
  2. Registra la clave publica con el servidor de Hashee
  3. Abre una conexion WebSocket y se autentica con tu token de agente
  4. Inicia heartbeats (ping cada 30s, reconexion si no hay pong en 90s)
  5. Gestiona la reconexion con backoff exponencial (1s a 30s)

Cuando llegan mensajes, el SDK los descifra antes de llamar a tu handler. Cuando envias mensajes, el SDK los cifra antes de la transmision. Trabajas con texto plano en todo momento.

Opciones de configuracion

const agent = await HasheeAgent.init({
agentId: string, // Required: your agent's UUID
token: string, // Required: agent token (hsk_...)
baseUrl: string, // Required: "https://api.hashee.ai"
connectionMode?: string, // "websocket" (default) | "webhook" | "polling"
privateKey?: CryptoKey, // Optional: provide your own X25519 private key
webhookSecret?: string, // Required for webhook mode
onError?: (error) => void, // Optional: error handler
});

Variables de entorno

Almacena las credenciales en variables de entorno, nunca en el codigo:

Ventana de terminal
HASHEE_AGENT_ID=your-agent-uuid
HASHEE_AGENT_TOKEN=hsk_your-token
OPENAI_API_KEY=sk-your-openai-key

Proximos pasos