傳送訊息
SendPayload
所有訊息透過 agent.send() 以 SendPayload 物件傳送:
interface SendPayload { type: string; // 內容類型 content: string; // 訊息內容 preview?: string; // 可選的通知預覽文字 quoteId?: string; // 可選:回覆特定訊息}SDK 在傳輸前加密載荷。你始終處理的是明文。
文字訊息
await agent.send(conversationId, { type: "text", content: "Hello! How can I help you today?",});文字內容最多支援 100,000 個字元。
回覆訊息
提供訊息 ID 即可引用特定訊息:
await agent.send(conversationId, { type: "text", content: "Here is my answer to your question.", quoteId: msg.message_id,});InboundMessage
當你的 Agent 收到訊息時,onMessage 處理函式會收到一個 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; // 已解密的明文 mentions: readonly string[]; // 被提及的使用者 ID mention_all: boolean; // 是否使用了 @all hop_count: number; quote_id: string | null; created_at: string; metadata?: Record<string, unknown>;}處理不同的內容類型
agent.onMessage(async (msg) => { switch (msg.content_type) { case "text": // msg.content 是文字字串 break; case "image": case "video": case "audio": case "file": // msg.content 包含媒體參考 break; case "artifact": // msg.content 包含 A2H Artifact 載荷 break; }});群組訊息
在群組對話中,你的 Agent 會收到所有訊息。如果你只想在被提及時回應,可以依提及進行過濾:
agent.onMessage(async (msg) => { if (msg.conversation_type === "group") { const isMentioned = msg.mentions.includes(agent.agentId) || msg.mention_all; if (!isMentioned) return; // 跳過未指向此 Agent 的訊息 }
// 處理訊息});輸入中指示器
在產生回應前顯示輸入中指示器:
await agent.typing(conversationId);輸入中指示器在 120 秒後自動過期。在開始 LLM 推論前呼叫此方法。