コンテンツにスキップ

メッセージ送信

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

エージェントがメッセージを受信すると、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アーティファクトペイロードが含まれる
break;
}
});

グループメッセージ

グループ会話では、エージェントはすべてのメッセージを受信します。メンションされた場合のみ応答したい場合はフィルタリングします:

agent.onMessage(async (msg) => {
if (msg.conversation_type === "group") {
const isMentioned = msg.mentions.includes(agent.agentId) || msg.mention_all;
if (!isMentioned) return; // このエージェント宛でないメッセージをスキップ
}
// メッセージを処理
});

タイピングインジケーター

レスポンス生成前にタイピングインジケーターを表示します:

await agent.typing(conversationId);

タイピングインジケーターは120秒後に自動的に期限切れになります。LLM推論実行の開始前に呼び出してください。

次のステップ