> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qoder.com/llms.txt
> Use this file to discover all available pages before exploring further.

# マルチターン会話

同一セッション内で複数のユーザーメッセージを送信するには、`QoderSDKClient` を使用します。永続接続を維持し、モデルの応答に基づいて次の発言を決定できます。一回限りでステートレスなクエリには `query()` を使用してください。[クイックスタート](/ja/cli/sdk/python/quick-start) を参照してください。

<div id="マルチメッセージセッション" />

## マルチメッセージセッション

`client.query(...)` を呼び出すたびに 1 ターンの入力を追加し、`client.receive_response()` で本ターンの応答が完了するまで消費します:

```python theme={null}
import anyio

from qoder_agent_sdk import (
    AssistantMessage,
    QoderAgentOptions,
    QoderSDKClient,
    ResultMessage,
    TextBlock,
    access_token_from_env,
)


async def main():
    options = QoderAgentOptions(auth=access_token_from_env())

    async with QoderSDKClient(options=options) as client:
        await client.query("What's the capital of France?")
        async for msg in client.receive_response():
            if isinstance(msg, AssistantMessage):
                for block in msg.content:
                    if isinstance(block, TextBlock):
                        print(f"Assistant: {block.text}")

        # 前のターンの応答に基づいて次の発言を決定
        await client.query("What's the population of that city?")
        async for msg in client.receive_response():
            if isinstance(msg, ResultMessage):
                print(f"Done: {msg.subtype}")


anyio.run(main)
```

<div id="セッションライフサイクルの管理" />

## セッションライフサイクルの管理

`QoderSDKClient` の接続ライフサイクルは呼び出し側が管理します。2 つの管理方法があります:

<div id="自動管理-推奨" />

<div id="自動管理推奨" />

### 自動管理 (推奨)

セッションのライフサイクルが特定の関数 / コードブロックにバインドされるシナリオに適しています:

```python theme={null}
async with QoderSDKClient(options=options) as client:
    await client.query("Hello")
    async for msg in client.receive_response():
        ...
# async with スコープを抜けると自動的に disconnect
```

<div id="手動管理" />

### 手動管理

クライアントが長寿命のオブジェクトに保持される場合、または外部条件 (タイムアウト、ユーザーキャンセルなど) によってクローズをトリガーする必要があるシナリオに適しています:

```python theme={null}
client = QoderSDKClient(options=options)
await client.connect()

try:
    await client.query("Hello")
    async for msg in client.receive_response():
        ...
finally:
    await client.disconnect()
```
