メインコンテンツまでスキップ

SSE ストリーミングガイド

AgenticStar Chat API の SSE ストリーミングレスポンスの詳細。接続ライフサイクル、レスポンスオブジェクト、エージェント拡張フィールド、エラーハンドリングを解説します。

概要

AgenticStar Chat API はレスポンスを SSE(Server-Sent Events)ストリーミングで配信します。各チャンクは OpenAI 互換の ChatCompletionChunk 形式で、エージェント固有の拡張フィールド(タスク情報、対話リクエスト等)が含まれます。

ポイント
  • Content-Type は text/event-stream; charset=utf-8
  • 各イベントは data: プレフィックス付きの JSON 行
  • ストリーム終了は data: [DONE] で通知
  • エージェントの長時間タスクでは、再接続・キャンセルのエンドポイントが利用可能

Chat API リファレンスに戻る

接続ライフサイクル

POST /v1/chat/completions へのリクエスト後、SSE 接続が確立され、以下の順序でイベントが配信されます。

SSE イベントフロー

: connected                          ← SSE接続確立コメント

data: {"createdAt":"...","model":"AGENTIC STAR","choices":[{"index":0,"delta":{"role":"assistant","messageInfo":{...}}}]}
↑ ロールチャンク(1回)

data: {"createdAt":"...","model":"AGENTIC STAR","choices":[{"index":0,"delta":{"content":"売上"}}]}
data: {"createdAt":"...","model":"AGENTIC STAR","choices":[{"index":0,"delta":{"content":"データ"}}]}
data: {"createdAt":"...","model":"AGENTIC STAR","choices":[{"index":0,"delta":{"content":"を..."}}]}
↑ コンテンツチャンク(複数回)

data: {"createdAt":"...","model":"AGENTIC STAR","choices":[{"index":0,"delta":{"content":"...","tasks":[{"messageId":"...","conversationId":"...","actionType":"execute","title":"データ分析","status":"running","timestamp":1234567890}]}}]}
↑ タスク実行情報を含むチャンク(複数回)

data: {"createdAt":"...","model":"AGENTIC STAR","choices":[{"index":0,"delta":{"content":"..."},"finishReason":"stop"}]}
↑ 最終チャンク(finishReason付き)

data: [DONE] ← ストリーム終了

各フェーズの詳細

フェーズ説明
: connectedSSE コメント行。接続が確立されたことを示します。
ロールチャンクもっとも最初の data イベント。delta に role: "assistant"messageInfo(conversationId, messageId)が含まれます。
コンテンツチャンクテキストコンテンツの差分。delta.content に追記テキストが含まれます。エージェントのタスク実行情報(delta.tasks)が含まれることもあります。複数回配信されます。
最終チャンクfinishReason が設定されたチャンク。status(実行状態)や deliverables(成果物)が含まれることがあります。
data: [DONE]ストリーム終了シグナル。この後 SSE 接続が閉じられます。

レスポンスヘッダー

ヘッダー説明
Content-Typetext/event-stream; charset=utf-8
X-Conversation-Id会話ID。新規会話の場合はサーバーが生成したIDが返されます。
X-Message-Idアシスタントの応答メッセージID。

ChatCompletionChunk オブジェクト

各 SSE イベントの data: 行に含まれるトップレベルオブジェクトです。

パラメータ説明
createdAtstringチャンクの生成日時(ISO 8601形式)
modelstringモデル名。"AGENTIC STAR"
choicesarrayChoiceオブジェクトの配列(要素数は常に1)
JSON
data: {
"createdAt": "2026-03-14T10:30:00.000Z",
"model": "AGENTIC STAR",
"choices": [{ ... }]
}

Choice オブジェクト

choices 配列の各要素です。AgenticStar では常に 1 要素のみ含まれます。

パラメータ説明
indexnumber常に 0
deltaobject差分データ。以下の Delta オブジェクトを参照。
finishReasonstring | null"stop"(正常完了)、"error"(エラー)、または null(ストリーミング中)
statusobject{ processing: boolean, unfinished: boolean } — エージェントの実行状態
deliverablesarrayエージェントが生成した成果物ファイルの配列

Delta オブジェクト

各チャンクの差分データです。チャンクの種類によって含まれるフィールドが異なります。

パラメータ出現説明
rolestring最初のみ最初のチャンクでのみ送信。"assistant" 固定。
messageInfoobject最初のみ{ conversationId, messageId } — メッセージ識別情報
contentstring複数回テキストコンテンツの差分
tasksarray複数回エージェントのタスク実行情報(タスク状態が更新されるたびに送信)
interactionobject条件付きユーザー対話リクエスト(選択肢提示等)

Delta 拡張フィールド

AgenticStar は OpenAI 互換のフォーマットにエージェント固有の拡張フィールドを追加しています。各フィールドはストリーミング中またはストリーム終了時に、それぞれ固有のタイミングで配信されます。

delta.tasks

エージェントのタスク実行情報。ツール実行・ファイル作成・検索などのアクション発生時に SSE チャンクの delta.tasks 配列として配信されます。Task データモデルの完全な定義はコンセプトガイドを参照してください。

SSE 配信コンテキスト

delta.tasks はタスク状態が更新されるたびに SSE チャンクに含まれます。1 チャンクに含まれる Task は通常 1 件です。delta.content と delta.tasks は同一チャンクに共存します。

SSE チャンク内での delta.tasks の位置:

JSON
{
"createdAt": "2026-03-14T15:00:53.059Z",
"model": "AGENTIC STAR",
"choices": [{
"index": 0,
"finishReason": null,
"delta": {
"content": "Processing local_assistant",
"tasks": [{
"messageId": "5b47837a-...",
"conversationId": "09071b95-...",
"callId": "0cf24f34-...",
"actionType": "tool_start",
"title": "local assistant",
"status": "in_progress",
"metadata": { "tool_name": "local_assistant", "call_id": "0cf24f34-...", "status": "starting" },
"timestamp": 1773499412706,
"createdAt": "2026-03-14T14:43:32.708Z",
"updatedAt": "2026-03-14T14:43:32.708Z"
}]
},
"status": { "processing": true, "unfinished": true }
}]
}

Task オブジェクト フィールド

パラメータ必須説明
messageIdstringタスクが属するメッセージのID
conversationIdstringタスクが属する会話のID
actionTypestring"tool_start" | "tool_result" | "search_result" | "command_execution" | "mcp_tool" | "file_operation" など
statusstring"pending" | "in_progress" | "completed" | "failed" | "success" | "error"
timestampnumberタスクのタイムスタンプ(Unix ミリ秒)
createdAtstringタスク作成日時(ISO 8601)
updatedAtstringタスク更新日時(ISO 8601)
titlestringタスクのタイトル(表示用)
descriptionstringタスクの詳細説明
callIdstring | nullツール呼び出しID。`tool_start` と対応する `tool_result` を紐付け。多くの actionType では `null`([詳細](connection-guide.md#task-data-model))
contentstringタスクの出力コンテンツ
metadataobjectタスクのメタデータ。actionType に応じて異なるフィールドを含む([詳細](connection-guide.md#task-data-model))
filesTaskFile[]タスクに関連するファイルの配列

TaskFile フィールド

パラメータ説明
filenamestringファイル名
fileTypestring"pdf", "image", "text", "pptx", "video", "other"
sizenumberファイルサイズ(バイト)
filepathstringファイルパス

TaskFile の例:

JSON
"files": [
{
"filename": "qr_code_decode_report.pdf",
"fileType": "pdf",
"size": 4201846,
"filepath": "/plans/{conversationId}/files/qr_code_decode_report.pdf"
}
]

代表的な actionType

actionType説明
tool_start特定ツールの開始通知(local_assistant / generate_video / Sandbox)
tool_result汎用ツール実行の結果。metadata.sub_event_type でツール種別を識別
search_resultWeb 検索の完了結果
command_executionシェルコマンド実行の完了結果
mcp_toolMCP ツール実行の結果(metadata なし)
file_operationファイル作成の完了通知

SSE ストリーム例: ツール実行ペア

チャンク 1: ツール開始(local_assistant)

JSON
"tasks": [{
"messageId": "5b47837a-...",
"conversationId": "09071b95-...",
"callId": "0cf24f34-bbd9-4833-88c4-d7f520ce3aae",
"actionType": "tool_start",
"title": "local assistant",
"description": "Processing local_assistant",
"status": "in_progress",
"metadata": {
"tool_name": "local_assistant",
"call_id": "0cf24f34-bbd9-4833-88c4-d7f520ce3aae",
"status": "starting"
},
"files": [],
"timestamp": 1773499412706,
"createdAt": "2026-03-14T14:43:32.708Z",
"updatedAt": "2026-03-14T14:43:32.708Z"
}]

チャンク 2: ツール完了(同じ callId / metadata.call_id で紐付け)

JSON
"tasks": [{
"messageId": "5b47837a-...",
"conversationId": "09071b95-...",
"callId": "call_T3GIhcQQft13amxdhcJ4cBDz",
"actionType": "tool_result",
"title": "success",
"description": "{'result': 'ok'}",
"status": "in_progress",
"metadata": {
"tool_name": "bash",
"call_id": "call_T3GIhcQQft13amxdhcJ4cBDz",
"sub_event_type": "bash_executed",
"command": "python3 script.py",
"exit_code": 0,
"stdout": "{'result': 'ok'}\n",
"status": "success",
"duration_ms": 24
},
"files": [],
"timestamp": 1773499362744,
"createdAt": "2026-03-14T14:42:42.747Z",
"updatedAt": "2026-03-14T14:42:42.747Z"
}]

delta.interaction

エージェントがユーザーに対話を要求する場合に送信されます。選択肢の提示(choice)と確認ダイアログ(confirmation)の2種類があります。

InteractionExtension フィールド

パラメータ必須説明
interactionTypestring"choice" | "confirmation"
contentstring対話メッセージの本文(choice / confirmation で送信)
optionsstring[]interactionType: "choice" の場合のみ。選択肢の配列。

interactionType 一覧

説明含まれるフィールド
choice選択肢を提示し、ユーザーに1つを選択させるcontent, options
confirmationユーザーに確認(承認/拒否)を求めるcontent

choice の例:

JSON
"interaction": {
"interactionType": "choice",
"content": "どの形式で出力しますか?",
"options": ["PDF形式", "Markdown形式", "HTML形式"]
}

confirmation の例:

JSON
"interaction": {
"interactionType": "confirmation",
"content": "このファイルを削除してもよろしいですか?"
}

choice.status

最終チャンクの Choice レベルに含まれるフィールドです。エージェントの実行状態を示します。

パラメータ説明
processingbooleanエージェントがバックグラウンドで処理を継続中かどうか。true の場合、再接続で後続イベントを受信可能。
unfinishedbooleanタスクが未完了かどうか。ユーザー対話の待機中や長時間処理中に true となる。
JSON
"status": { "processing": false, "unfinished": false }

choice.deliverables

最終チャンクの Choice レベルに含まれるフィールドです。エージェントが生成した成果物ファイルの一覧です。

DeliverableFile フィールド

パラメータ説明
filenamestringファイル名
filepathstringファイルパス(ダウンロード用の相対パス)
fileTypestring"pdf", "image", "text", "pptx", "video", "other"
sourcestring"agent"(エージェント生成)または "user"(ユーザーアップロード)。
isPrimaryboolean主要成果物かどうか
createdAtstring作成日時(ISO 8601形式)
JSON
"deliverables": [{
"filename": "report.pdf",
"filepath": "/files/output/report.pdf",
"fileType": "pdf",
"source": "agent",
"isPrimary": true,
"createdAt": "2026-03-14T10:30:05.000Z"
}]

再接続

進行中の会話の SSE ストリームに再接続します。ネットワーク切断後の復帰に使用します。

GET /v1/chat/completions/{conversationId}

パスパラメータ

パラメータ説明
conversationIdstring再接続する会話のID

使用例

curl
# 進行中の会話に再接続
curl -N https://api.agenticstar.jp/v1/chat/completions/550e8400-e29b-41d4-a716-446655440000 \
-H "Authorization: Bearer $TOKEN"

再接続のタイミング

シナリオ説明
ネットワーク切断クライアント側のネットワーク障害後、X-Conversation-Id を使って再接続
備考

再接続は進行中の会話にのみ可能です。終了済みの会話には 404 Not Found が返ります。終了済みの会話の結果は、リソース API(GET /v1/conversations/{conversationId}/messages)で取得できます。

キャンセル

進行中の会話をキャンセルし、エージェントの実行を中断します。

POST /v1/chat/completions/{conversationId}/cancel

使用例

curl
curl -X POST https://api.agenticstar.jp/v1/chat/completions/550e8400-e29b-41d4-a716-446655440000/cancel \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json"

レスポンス

JSON
{
"conversationId": "550e8400-e29b-41d4-a716-446655440000",
"cancelled": true
}

エラーハンドリング

エラーは発生タイミングによって 2 種類のフォーマットで返されます。

ストリーム開始前のエラー(HTTP エラー)

SSE 接続が確立される前に発生するエラーです。通常の HTTP レスポンスとして JSON 形式で返されます。

ステータス説明
400リクエストパラメータ不正(model, messages, stream等)
401認証トークンが無効または未指定
403chat:exec スコープが不足
409指定した会話が現在処理中

HTTP 400 エラーレスポンス:

JSON
{
"error": {
"code": 400,
"message": "Parameter 'model' is required"
}
}

ストリーム中のエラー(SSE エラーチャンク)

SSE ストリーミング中にバックエンドで発生したエラーです。finishReason が "error" に設定された ChatCompletionChunk として配信されます。

data: {
"createdAt": "2026-03-14T10:30:00.000Z",
"model": "AGENTIC STAR",
"choices": [{
"index": 0,
"delta": { "content": "An error occurred..." },
"finishReason": "error"
}]
}

data: [DONE]

リカバリー戦略

エラー種別推奨アクション
HTTP エラー(400/401/403)リクエストパラメータまたは認証情報を確認して再送信
HTTP 409 Conflict会話の処理完了を待つか、キャンセルしてから再送信
SSE エラーチャンク新しいメッセージで会話を再開するか、別の会話を開始
接続断GET /v1/chat/completions/{conversationId} で再接続を試行

完全なレスポンス例

ロールチャンク → コンテンツチャンク → 最終チャンクの典型的なストリーミングシーケンスです。

JSON
// 1. ロールチャンク(最初の1回)
data: {
"createdAt": "2026-03-14T10:30:00.000Z",
"model": "AGENTIC STAR",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"messageInfo": {
"conversationId": "550e8400-e29b-41d4-a716-446655440000",
"messageId": "660f9511-f3ac-52e5-b827-557766551111"
}
}
}]
}

// 2. コンテンツチャンク(複数回)
data: {
"createdAt": "2026-03-14T10:30:00.123Z",
"model": "AGENTIC STAR",
"choices": [{
"index": 0,
"delta": {
"content": "売上データの分析結果です。"
}
}]
}

// 2a. タスク実行情報を含むコンテンツチャンク
data: {
"createdAt": "2026-03-14T10:30:01.500Z",
"model": "AGENTIC STAR",
"choices": [{
"index": 0,
"delta": {
"content": "データを分析しています...",
"tasks": [{
"messageId": "660f9511-f3ac-52e5-b827-557766551111",
"conversationId": "550e8400-e29b-41d4-a716-446655440000",
"actionType": "tool_start",
"status": "in_progress",
"title": "local assistant",
"metadata": { "tool_name": "local_assistant", "status": "starting" },
"timestamp": 1710410001500,
"createdAt": "2026-03-14T10:30:01.500Z",
"updatedAt": "2026-03-14T10:30:01.500Z"
}]
},
"status": { "processing": true, "unfinished": true }
}]
}

// 3. 最終チャンク(成果物・ステータス付き)
data: {
"createdAt": "2026-03-14T10:30:05.456Z",
"model": "AGENTIC STAR",
"choices": [{
"index": 0,
"delta": {
"content": "以上が分析結果です。",
"tasks": [{
"messageId": "660f9511-f3ac-52e5-b827-557766551111",
"conversationId": "550e8400-e29b-41d4-a716-446655440000",
"actionType": "tool_result",
"status": "in_progress",
"title": "データ分析",
"content": "分析が完了しました。",
"timestamp": 1710410000000,
"createdAt": "2026-03-14T10:30:05.400Z",
"updatedAt": "2026-03-14T10:30:05.400Z"
}]
},
"finishReason": "stop",
"status": { "processing": false, "unfinished": false },
"deliverables": [{
"filename": "report.pdf",
"filepath": "/files/output/report.pdf",
"fileType": "pdf",
"source": "agent",
"isPrimary": true,
"createdAt": "2026-03-14T10:30:05.000Z"
}]
}]
}

// 4. ストリーム終了
data: [DONE]