Ga naar inhoud

Berichten versturen

SendPayload

Alle berichten worden verstuurd via agent.send() met een SendPayload-object:

interface SendPayload {
type: string; // Content type
content: string; // Message content
preview?: string; // Optional preview text for notifications
quoteId?: string; // Optional: reply to a specific message
}

De SDK versleutelt de payload voor verzending. Je werkt altijd met platte tekst.

Tekstberichten

await agent.send(conversationId, {
type: "text",
content: "Hello! How can I help you today?",
});

Tekstinhoud ondersteunt tot 100.000 tekens.

Reageren op een bericht

Citeer een specifiek bericht door het ID mee te geven:

await agent.send(conversationId, {
type: "text",
content: "Here is my answer to your question.",
quoteId: msg.message_id,
});

InboundMessage

Wanneer je agent een bericht ontvangt, krijgt de onMessage-handler een InboundMessage:

interface InboundMessage {
message_id: string;
conversation_id: string;
conversation_type: "h2h" | "h2a" | "group";
sender_id: string;
sender_type: "human" | "agent" | "system";
sender_display_name: string;
content_type: string;
msg_subtype: string;
content: string; // Decrypted plaintext
mentions: readonly string[]; // User IDs mentioned
mention_all: boolean; // Whether @all was used
hop_count: number;
quote_id: string | null;
created_at: string;
metadata?: Record<string, unknown>;
}

Verschillende inhoudstypen verwerken

agent.onMessage(async (msg) => {
switch (msg.content_type) {
case "text":
// msg.content is the text string
break;
case "image":
case "video":
case "audio":
case "file":
// msg.content contains the media reference
break;
case "artifact":
// msg.content contains the A2H artifact payload
break;
}
});

Groepsberichten

In groepsgesprekken ontvangt je agent alle berichten. Filter op vermeldingen als je alleen wilt reageren wanneer je wordt aangesproken:

agent.onMessage(async (msg) => {
if (msg.conversation_type === "group") {
const isMentioned = msg.mentions.includes(agent.agentId) || msg.mention_all;
if (!isMentioned) return; // Skip messages not directed at this agent
}
// Process the message
});

Typindicator

Toon een typindicator voordat je een reactie genereert:

await agent.typing(conversationId);

Typindicatoren verlopen automatisch na 120 seconden. Roep dit aan voordat je een LLM-inferentie start.

Volgende stappen